Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重构到模板类 我是C++语言和学习模板的新手。 我需要将下面的类重构为模板化类。除了继承的基类之外,下面的类中有多个重复的代码。LineDerived类和DotMatrixderived类的功能相同,只是它们的基类和虚函数不同。对于RealLine和RealDotMatrix类也是如此 class LineDerived: public printer::printerType::Line { public: LineDerived(const std::wstring& str1) : _str(str1) {} virtual const std::wstring getData() override { return _str; } private: const std::wstring _str; }; class DotMatrixderived: public printer::printerType::DotMatrix { public: DotMatrixderived(const std::wstring& str1) : _str(str1) {} virtual const std::wstring getData() override { return _str; } private: const std::wstring _str; }; class RealLine : public printer::system::LineSystem { public: RealLine (const std::wstring& str1) : LineSystem(boost::make_shared<LineDerived>(str1)) { } virtual method1() { .. }; virtual method2() { .. }; }; class RealDotMatrix : public printer::system::DotMatrixSystem { public: RealDotMatrix (const std::wstring& str1) : DotMatrixSystem(boost::make_shared<DotMatrixderived>(str1)) { } virtual method1() { .. }; virtual method2() { .. }; }; // Calling Part: RealLine line(strData); line.method1(); line.method2(); RealDotMatrix matrix(strData); matrix.method1(); matrix.method2() class LineDerived:public printer::printerType::Line { 公众: LineDerived(const std::wstring和str1):\u str(str1){} 虚拟常量std::wstring getData()重写{return\u str;} 私人: const std::wstring_str; }; 类DotMatrix派生:公共打印机::打印机类型::DotMatrix { 公众: 点矩阵派生(const std::wstring和str1):\u str(str1){ 虚拟常量std::wstring getData()重写{return\u str;} 私人: const std::wstring_str; }; 类RealLine:public printer::system::LineSystem { 公众: RealLine(const std::wstring和str1): 线路系统(boost::使_共享(str1)) { } 虚拟方法1(){..}; 虚拟方法2(){..}; }; 类RealDotMatrix:public printer::system::DotMatrixSystem { 公众: RealDotMatrix(常数std::wstring和str1): DotMatrixSystem(boost::使_共享(str1)) { } 虚拟方法1(){..}; 虚拟方法2(){..}; }; //呼叫部分: 实线(strData); line.method1(); line.method2(); RealDotMatrix矩阵(strData); matrix.method1(); matrix.method2()_C++_Templates_Refactoring - Fatal编程技术网

重构到模板类 我是C++语言和学习模板的新手。 我需要将下面的类重构为模板化类。除了继承的基类之外,下面的类中有多个重复的代码。LineDerived类和DotMatrixderived类的功能相同,只是它们的基类和虚函数不同。对于RealLine和RealDotMatrix类也是如此 class LineDerived: public printer::printerType::Line { public: LineDerived(const std::wstring& str1) : _str(str1) {} virtual const std::wstring getData() override { return _str; } private: const std::wstring _str; }; class DotMatrixderived: public printer::printerType::DotMatrix { public: DotMatrixderived(const std::wstring& str1) : _str(str1) {} virtual const std::wstring getData() override { return _str; } private: const std::wstring _str; }; class RealLine : public printer::system::LineSystem { public: RealLine (const std::wstring& str1) : LineSystem(boost::make_shared<LineDerived>(str1)) { } virtual method1() { .. }; virtual method2() { .. }; }; class RealDotMatrix : public printer::system::DotMatrixSystem { public: RealDotMatrix (const std::wstring& str1) : DotMatrixSystem(boost::make_shared<DotMatrixderived>(str1)) { } virtual method1() { .. }; virtual method2() { .. }; }; // Calling Part: RealLine line(strData); line.method1(); line.method2(); RealDotMatrix matrix(strData); matrix.method1(); matrix.method2() class LineDerived:public printer::printerType::Line { 公众: LineDerived(const std::wstring和str1):\u str(str1){} 虚拟常量std::wstring getData()重写{return\u str;} 私人: const std::wstring_str; }; 类DotMatrix派生:公共打印机::打印机类型::DotMatrix { 公众: 点矩阵派生(const std::wstring和str1):\u str(str1){ 虚拟常量std::wstring getData()重写{return\u str;} 私人: const std::wstring_str; }; 类RealLine:public printer::system::LineSystem { 公众: RealLine(const std::wstring和str1): 线路系统(boost::使_共享(str1)) { } 虚拟方法1(){..}; 虚拟方法2(){..}; }; 类RealDotMatrix:public printer::system::DotMatrixSystem { 公众: RealDotMatrix(常数std::wstring和str1): DotMatrixSystem(boost::使_共享(str1)) { } 虚拟方法1(){..}; 虚拟方法2(){..}; }; //呼叫部分: 实线(strData); line.method1(); line.method2(); RealDotMatrix矩阵(strData); matrix.method1(); matrix.method2()

重构到模板类 我是C++语言和学习模板的新手。 我需要将下面的类重构为模板化类。除了继承的基类之外,下面的类中有多个重复的代码。LineDerived类和DotMatrixderived类的功能相同,只是它们的基类和虚函数不同。对于RealLine和RealDotMatrix类也是如此 class LineDerived: public printer::printerType::Line { public: LineDerived(const std::wstring& str1) : _str(str1) {} virtual const std::wstring getData() override { return _str; } private: const std::wstring _str; }; class DotMatrixderived: public printer::printerType::DotMatrix { public: DotMatrixderived(const std::wstring& str1) : _str(str1) {} virtual const std::wstring getData() override { return _str; } private: const std::wstring _str; }; class RealLine : public printer::system::LineSystem { public: RealLine (const std::wstring& str1) : LineSystem(boost::make_shared<LineDerived>(str1)) { } virtual method1() { .. }; virtual method2() { .. }; }; class RealDotMatrix : public printer::system::DotMatrixSystem { public: RealDotMatrix (const std::wstring& str1) : DotMatrixSystem(boost::make_shared<DotMatrixderived>(str1)) { } virtual method1() { .. }; virtual method2() { .. }; }; // Calling Part: RealLine line(strData); line.method1(); line.method2(); RealDotMatrix matrix(strData); matrix.method1(); matrix.method2() class LineDerived:public printer::printerType::Line { 公众: LineDerived(const std::wstring和str1):\u str(str1){} 虚拟常量std::wstring getData()重写{return\u str;} 私人: const std::wstring_str; }; 类DotMatrix派生:公共打印机::打印机类型::DotMatrix { 公众: 点矩阵派生(const std::wstring和str1):\u str(str1){ 虚拟常量std::wstring getData()重写{return\u str;} 私人: const std::wstring_str; }; 类RealLine:public printer::system::LineSystem { 公众: RealLine(const std::wstring和str1): 线路系统(boost::使_共享(str1)) { } 虚拟方法1(){..}; 虚拟方法2(){..}; }; 类RealDotMatrix:public printer::system::DotMatrixSystem { 公众: RealDotMatrix(常数std::wstring和str1): DotMatrixSystem(boost::使_共享(str1)) { } 虚拟方法1(){..}; 虚拟方法2(){..}; }; //呼叫部分: 实线(strData); line.method1(); line.method2(); RealDotMatrix矩阵(strData); matrix.method1(); matrix.method2(),c++,templates,refactoring,C++,Templates,Refactoring,我想做一些事情,其中只有一个派生类的类型为打印机类型,即Line和DotMatrix,类似于“派生”和“派生我花了一段时间才使您的示例成为一个工作示例,但最后我想到了以下内容: #include <string> #include <memory> // don't have boost now so I use stl to make the example work namespace boost { using std::make_shared; using std

我想做一些事情,其中只有一个派生类的类型为打印机类型,即Line和DotMatrix,类似于“派生”和“派生我花了一段时间才使您的示例成为一个工作示例,但最后我想到了以下内容:

#include <string>
#include <memory>
// don't have boost now so I use stl to make the example work
namespace boost { using std::make_shared; using std::shared_ptr; }

namespace printer::printerType {
    class PrinterTypeBase {};

    class Line : public PrinterTypeBase {
    public:
        virtual const std::wstring getData() = 0;
        virtual ~Line(){}
    }; 

    class DotMatrix : public PrinterTypeBase {
    public:
        virtual const std::wstring getData() = 0;
        virtual ~DotMatrix(){}
    };
}

namespace printer::system {
    class LineSystem
    {
    public:
        LineSystem(boost::shared_ptr<printer::printerType::PrinterTypeBase>);

    };
    class DotMatrixSystem
    {
    public:
        DotMatrixSystem(boost::shared_ptr<printer::printerType::PrinterTypeBase>);
    };
}

template <typename T>
class Derived: public T::printerType
{
public:
    Derived(const std::wstring& str1) : _str(str1) {}
    virtual const std::wstring getData() override { return _str; }

private:
    const std::wstring _str;
};

template <typename T>
class Real : public T::systemType
{
public:
    Real(const std::wstring& str1) : 
    T::systemType(boost::make_shared<typename T::printerType>(str1))
    {
    }

    virtual void method1() { /*..*/ }
    virtual void method2() { /*..*/ }
    virtual ~Real() {}
};



struct DotMatrix
{
    using printerType = printer::printerType::DotMatrix;
    using systemType = printer::system::DotMatrixSystem;
};

struct Line
{
    using printerType = printer::printerType::Line;
    using systemType = printer::system::LineSystem;
};


int main(int, char**)
{
    const std::wstring strData;
    // Calling Part:
    Real<Line> line(strData);
    line.method1();
    line.method2();

    Real<DotMatrix> matrix(strData);
    matrix.method1();
    matrix.method2();
}
#包括
#包括
//现在没有boost,所以我使用stl使示例工作
命名空间提升{使用std::make_shared;使用std::shared_ptr;}
名称空间打印机::printerType{
类PrinterTypeBase{};
类行:公共PrinterTypeBase{
公众:
虚拟常量std::wstring getData()=0;
虚拟~Line(){}
}; 
类DotMatrix:公共PrinterTypeBase{
公众:
虚拟常量std::wstring getData()=0;
虚~DotMatrix(){}
};
}
名称空间打印机::系统{
类线系统
{
公众:
线路系统(boost::shared_ptr);
};
类DotMatrix系统
{
公众:
DotMatrixSystem(boost::shared_ptr);
};
}
模板
派生类:public T::printerType
{
公众:
派生(常量std::wstring和str1):\u str(str1){
虚拟常量std::wstring getData()重写{return\u str;}
私人:
const std::wstring_str;
};
模板
类Real:public T::systemType
{
公众:
实数(常量标准::wstring和str1):
T::systemType(boost::使_共享(str1))
{
}
虚拟void method1(){/*..*/}
虚拟void method2(){/*..*/}
虚~实(){}
};
结构点矩阵
{
使用printerType=printer::printerType::DotMatrix;
使用systemType=printer::system::DotMatrixSystem;
};
结构线
{
使用printerType=printer::printerType::Line;
使用systemType=printer::system::LineSystem;
};
int main(int,char**)
{
常量std::wstring strData;
//呼叫部分:
实线(strData);
line.method1();
line.method2();
实矩阵(strData);
matrix.method1();
方法2();
}

您的问题到底是什么?我想做一些事情,其中只有一个派生类的类型为printer Type,类似于derived和derived,而real类的类型相同。因此,最后的调用应该类似于real line(strData)和real dot(strData)。