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”.
Constructor called Virtual method called Derived class Constructor called Derived class Virtual method called Virtual method called
- 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 verison 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 verison of bark is called.
NOTE: It is highly recommended to avoid calling virtual methods from constructor/destructor.
This article is contributed by Yash Singla. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- What happens when a virtual function is called inside a non-virtual function in C++
- Difference between Virtual function and Pure virtual function in C++
- C/C++ program for calling main() in main()
- Virtual Destructor
- Virtual Function in C++
- Virtual destruction using shared_ptr in C++
- Virtual base class in C++
- Pure virtual destructor in C++
- Can virtual functions be private in C++?
- Can virtual functions be inlined?
- Can static functions be virtual in C++?
- Advanced C++ | Virtual Constructor
- Default arguments and virtual function
- Advanced C++ | Virtual Copy Constructor
- Virtual functions in derived classes
- Virtual Functions and Runtime Polymorphism in C++ | Set 1 (Introduction)
- Pure Virtual Functions and Abstract Classes in C++
- C++ interview questions on virtual function and abstract class
- Methods vs. Functions in C++ with Examples
- Default Methods in C++ with Examples