Can a template template be used in the constructor?

Possible duplicate:
Calling virtual functions inside constructors

I have a Shape class and its Sphere subclass:

//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; }

      

Now, how can I handle the subclass stuff in the Shape constructor? I could have resorted to template template template , but I would have to call a pure virtual function in the constructor; I tried and he didn't like the compiler

Edit :

I decided to move the constructor stuff into a new "init" method and the virtual method will be "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*/
    }

      

Basically template template template

The client will write:

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

      

Then I have an answer: it is not possible to apply this pattern in a constructor, at least in C ++

+3


source to share


1 answer


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

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

   /*Some stuff proper to Shape*/
}

      

Subclassing cannot be enforced until the subclass is created, so it must go in the constructor of the subclass. Later stuff can then come in a function called the constructor of the subclass.



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();
}

      

+1


source







All Articles