Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling virtual function of derived class from base class constructor?

Tags:

c++

I´m trying to accomplish the same which is described in a previous question:

virtual function call from base class

But, my real question is:

What if f() is the constructor in the Base class? Which g() will be called? I don´t know if I am doing wrong, but in my program it seems to be that is the opposite.

Taking the same variables from the previous question, a code which shows such

behavior would look like this:

Class Base

{   

    Base(){g();};

    virtual void g(){//Do some Base related code;}

};



Class Derived : public Base

{   

    Derived(){};

    virtual void g(){//Do some Derived related code};

};



int main()

{

    Derived newDerived;

    return 0;  

}

Update:

Thanx to Naveen.

He provided me a page which contains all related information about this topic.

I´ll let you know the link here:

parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.6

like image 617
Jesufer Vn Avatar asked Jul 03 '11 06:07

Jesufer Vn


People also ask

Why can't I call virtual functions from the base class constructor?

In short, objects are constructed from the base up to the derived. So when you try to call a virtual function from the base class constructor, overriding from derived classes hasn't yet happened because the derived constructors haven't been called yet. Share Improve this answer

How to call a derived class’s function from a base class?

When you use a pointer or a reference to the base class to refer to a derived class object, you can call a virtual function for that object and have it run the derived class’s version of the function. In C++, once a member function is declared as a virtual function in a base class, it becomes virtual in every class derived from that base class.

Why do virtual functions never go down into derived classes?

Even when you clearly try to create object of MyClass2, but during base class construction, virtual functions never go down into derived classes. Because base class constructors execute before derived class constructors, derived class data members have not been initialized when base class constructors run.

How do you call a virtual method from a base class?

So a call to the virtual method in the constructor will call the Base class' virtual method. Or if it has no implementation at that level, it'll produce a pure virtual method call. Once the Base is fully constructed, the compiler starts building Derived class, and overrides the method pointers to point Derived class' implementation.


1 Answers

Even though it's a virtual function, the base's version will get called since the derived class isn't fully constructed yet. The base class constructor is called before the derived class constructor, so if the derived virtual function were to get called, it would be with an incompletely-initialized instance - a possible (probably) recipe for disaster.

like image 154
Jim Buck Avatar answered Sep 21 '22 00:09

Jim Buck