Calling virtual methods in constructor/destructor in C++ - GeeksforGeeks

18 Aug, 2021
Prerequisite: Virtual Function in C++Calling virtual functions from a constructor or destructor is considered dangerous most of the times and must be avoided whenever possible. All the C++ implementations need to call the version of the function defined at the level of the hierarchy in the current constructor and not further. You can call a virtual function in a constructor. The Objects are constructed from the base up, “base before derived”. 
CPP
// CPP program to illustrate// calling virtual methods in// constructor/destructor#include<iostream>using namespace std; class dog{public:    dog()    {        cout<< "Constructor called" <<endl;        bark() ;    }     ~dog()    {        bark();    }     virtual void bark()    {        cout<< "Virtual method called" <<endl;    }     void seeCat()    {        bark();    }}; class Yellowdog : public dog{public:        Yellowdog()        {            cout<< "Derived class Constructor called" <<endl;        }        void bark()        {            cout<< "Derived class Virtual method called" <<endl;        }}; int main(){    Yellowdog d;    d.seeCat();}
Output:  
Constructor called
Virtual method called
Derived class Constructor called
Derived class Virtual method called
Virtual method called
Explanation:  
bark method in dog class is invoked in constructor as well as destructor.
When the object of Yellowdog is created, constructor of dog and then of Yellowdog is called and reverse order of calling for destructor.
Although, bark method is virtual method but when it is called inside constructor it will behave as non-virtual method because by the time constructor of dog(base) class is called as in above code, Yellowdog(derived) class is not constructed by that time.
Therefore, it is dangerous to call the member function of class whose object is not constructed yet and compiler calls the dog class version of bark method. And same is with the destructor, when object ‘d’ of Yellowdog gets destroyed, destructor of Yellowdog class is called first and then destructor for dog class is called but by this time Yellowdog is already destroyed, hence dog class version of bark is called.
NOTE: It is highly recommended to avoid calling virtual methods from constructor/destructor. Quiz on Virtual FunctionsThis article is contributed by Yash Singla. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
abhishek0719kadiyan
cpp-inheritance
cpp-virtual
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Sorting a vector in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
Convert string to char array in C++
Iterators in C++ STL
Inline Functions in C++
List in C++ Standard Template Library (STL)
Multithreading in C++