c++ - 是否可以在构造函数中使用模板方法模式?

标签 c++ constructor virtual template-method-pattern

<分区>

Possible Duplicate:
Calling virtual functions inside constructors

我有一个类 Shape 及其子类 Sphere :

//Shape : 

class Shape
{
    public:
        Shape(const string& name);
        virtual ~Shape();

        virtual string getName();

    protected:

        string mName;

};

Shape::Shape(const string& name) : mName(name)
{
   /*Some stuff proper to Shape*/

   /*Some stuff proper to subclass (sphere)*/

   /*Some stuff proper to Shape*/
}

Shape::~Shape(){}

string Shape::getName(){ return mName; }


//Sphere :

class Sphere : public Shape
{
    public:
        Sphere(const string& name, const float radius);
        virtual ~Sphere();

        virtual string getRadius();

    protected:

        float mRadius;
}

Sphere::Sphere(const string& name, const float radius) : Shape(name), mRadius(radius)
{
   /*Some stuff*/
}

Sphere::~Sphere(){}

float Sphere::getRadius(){ return mRadius; }

现在,我如何处理 Shape 构造函数中的子类内容?我可以诉诸 template method pattern但是我将被迫在构造函数中调用纯虚函数;我试过了,编译器不喜欢它

编辑:

我选择将构造函数的东西移动到一个新方法“init”中,虚拟方法将是“subInit”:

//Shape : 

class Shape
{
    public:
        Shape(const string& name);
        virtual ~Shape();

        virtual string getName();

        virtual void init();

    protected:

        string mName;

        virtual void subInit() = 0;

};

Shape::Shape(const string& name) : mName(name){}

Shape::~Shape(){}

string Shape::getName(){ return mName; }

void Shape::init()
{
   /*Some stuff proper to Shape*/

   /*Some stuff proper to subclass (sphere)*/
   /*Call to the pure virtual function subInit*/

   subInit();

   /*Some stuff proper to Shape*/
}

//Sphere : 

class Sphere : public Shape
{
    public:
         Sphere(const string& name, const float radius);
         virtual ~Sphere();

         virtual string getRadius();

        protected:

            float mRadius;

            void subInit();
    }

    Sphere::Sphere(const string& name, const float radius) : Shape(name),mRadius(radius)
    {}

    Sphere::~Sphere(){}

    float Sphere::getRadius(){ return mRadius; }

    Sphere::subInit()
    {
       /*Some stuff previously in the constructor*/
    }

基本上就是模板方法模式

客户会写:

Shape* sphere = new Sphere();
sphere->init();

然后我有了答案:不可能在构造函数中应用这种模式,至少在 C++ 中是这样

最佳答案

Shape::Shape(const string& name) : mName(name)
{
   /*Some stuff proper to Shape*/

   /*Some stuff proper to subclass (sphere)*/

   /*Some stuff proper to Shape*/
}

子类本身的东西只有在子类存在后才能运行,所以应该放在子类的构造函数中。后面的东西可以放在子类构造函数调用的函数中。

Shape::Shape(const string& name) : mName(name)
{
   /*Some stuff proper to Shape*/
}

void Shape::finishConstruction()
{   
   /*Some stuff proper to Shape*/
}

Sphere::Sphere(const string& name, const float radius)
: Shape(name), mRadius(radius)
{
    /*Some stuff proper to subclass (sphere)*/

    finishConstruction();
}

关于c++ - 是否可以在构造函数中使用模板方法模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14552412/

相关文章:

c++ - Vector2i 不想合作 :D

c++ - 对象构造期间缓冲区太小。觉得跟strcpy_s()有关系

c++ - 覆盖 Direct3D 接口(interface)?

c++ - 在构造函数中引用未初始化的对象

java - 出现错误 :(20, 44​​) java: 构造函数 XXX 无法应用于给定类型;

c++ - 虚拟公共(public)继承?需要帮助理解代码

C++:获取与继承和接口(interface)相关的编译错误

c++ - 阐明 C/C++ 中的悬挂指针

c++ - boost::asio 在从串行端口读取时阻塞 std::cout

c++ - 以&(&符号)结尾的原型(prototype)是什么意思