1

I have this layout

class Base {
   public:
   virtual void Initialize() { // base Implementation }
   Base() { Initialize(); }
};

class der_1 : public Base
{
    public:
    der_1() : Base() {}
    virtual void Initialize() { // der_1 Implementation }
};

class der_2 : public Base
{
    public:
    der_2() : Base() {}
    virtual void Initialize() { // der_2 Implementation }
};

Now, whenever I create a new object of class der_1 or der_2, I will end up calling the base implementation of Initialize(). Apparently, I can't call a virtual function while the object is being created.

As of now, I am calling the Initialize function after I create the object of type der_1 or der_2, which doesn't seem a correct practice to me as that will couple the Initialize function call to each time an object is created.

Can someone suggest me better alternatives?

sud03r
  • 19,109
  • 16
  • 77
  • 96
  • 4
    I'm not going to waste my time writing a proper answer, as I'm sure this has been answered 100 times before. You can't call virtual functions in the constructor. Make Initialize a non-virtual function and call it in each constructor, and it will be fine. Or call it after the constructor. One of those two... – Mats Petersson Mar 04 '13 at 12:52
  • Duplicate of: http://stackoverflow.com/questions/496440/c-virtual-function-from-constructor Or: http://stackoverflow.com/questions/962132/calling-virtual-functions-inside-constructors – Mats Petersson Mar 04 '13 at 12:53
  • @sud03r: What is the point of creating a `virtual` method for initialization when you are writing the constructor already ? Just do the work in the constructor and be done with it! – Matthieu M. Mar 04 '13 at 12:55
  • It has been asked 100 times. Just take a look at the related post to the right over there =====> –  Mar 04 '13 at 12:55
  • Move the code from `Initialize` into the constructor. Delete `Initialize`. Job done. – Peter Wood Mar 04 '13 at 12:59
  • Relevant entries about this from C++Faq Lite: [here](http://www.parashift.com/c++-faq-lite/calling-virtuals-from-ctors.html) and [here](http://www.parashift.com/c++-faq-lite/calling-virtuals-from-ctor-idiom.html) – jrok Mar 04 '13 at 13:00
  • 2
    @Everyone I am sorry about asking that again. I should have googled it well. Will close it. – sud03r Mar 04 '13 at 13:03
  • @MatsPetersson I know what you mean, but just a minor quibble, you can call virtual functions from constructors (and if you're calling them indirectly, via another function, you are probably doing so using dynamic dispatch)...it's just that that dispatch goes to the currently constructing subobject (i.e. the vptr points to the base vtable). it's more accurate to describe it that way if you're trying to be pedagogical. – Stephen Lin Mar 04 '13 at 14:23

1 Answers1

0

During the constructor call, the object "is" still only an instance of the base class, so it does know about your overloaded Initialize() function.

There are some suggestions for dealing with the situation here:

C++ virtual function from constructor

Community
  • 1
  • 1
hannes
  • 966
  • 9
  • 13