In C++ you can have virtual destructors but NOT virtual constructors. This is because when an object has virtual functions it must have an associated v-table which keeps track of the addresses to the virtual functions for that class. That would mean that an object must be instantiated firstly before a v-table for that object could exist. Since constructors are intended to instantiate and create the object there can be no virtual constructor.
On the other hand a virtual destructor is allowed and should be used when ever there are virtual methods in the base class. The misuse of virtual destructors can lead to memory leaks and bad side effects
If the destructor in the base class is not made virtual, then an object that might have been declared of type base class and instance of child class would simply call the base class destructor without calling the derived class destructor.- Hence, by making the destructor in the base class virtual, we ensure that the derived class destructor gets called before the base class destructor.
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Can you answer this question?
Write Answer1 Answers