Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to use the template method pattern in the constructor?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/962132/calling-virtual-functions-inside-constructors">Calling virtual functions inside constructors</a> </p> </blockquote> <p>I have a class Shape and its subclass Sphere :</p> <pre class="lang-cpp prettyprint-override"><code>//Shape : class Shape { public: Shape(const string&amp; name); virtual ~Shape(); virtual string getName(); protected: string mName; }; Shape::Shape(const string&amp; 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&amp; name, const float radius); virtual ~Sphere(); virtual string getRadius(); protected: float mRadius; } Sphere::Sphere(const string&amp; name, const float radius) : Shape(name), mRadius(radius) { /*Some stuff*/ } Sphere::~Sphere(){} float Sphere::getRadius(){ return mRadius; } </code></pre> <p>Now, how can I handle the subclass stuff in the Shape constructor ? I could resort to <a href="http://en.wikipedia.org/wiki/Template_method_pattern" rel="nofollow noreferrer">template method pattern</a> but I would be forced to call a pure virtual function in the constructor; I tried and the compiler didn't like it</p> <p><strong>Edit</strong> : </p> <p>I chose to move the constructor stuff in a new method, 'init', and the virtual method will be 'subInit':</p> <pre class="lang-cpp prettyprint-override"><code>//Shape : class Shape { public: Shape(const string&amp; name); virtual ~Shape(); virtual string getName(); virtual void init(); protected: string mName; virtual void subInit() = 0; }; Shape::Shape(const string&amp; 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&amp; name, const float radius); virtual ~Sphere(); virtual string getRadius(); protected: float mRadius; void subInit(); } Sphere::Sphere(const string&amp; name, const float radius) : Shape(name),mRadius(radius) {} Sphere::~Sphere(){} float Sphere::getRadius(){ return mRadius; } Sphere::subInit() { /*Some stuff previously in the constructor*/ } </code></pre> <p>It's basicaly the template method pattern</p> <p>The client will write : </p> <pre class="lang-cpp prettyprint-override"><code>Shape* sphere = new Sphere(); sphere-&gt;init(); </code></pre> <p>Then I have my answer : its impossible to apply this pattern in the constructor, at least in C++</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload