Yahoo Interview Question
Software Engineer / DevelopersSee the concept of Virtual Destructor comes into picture when atleast one of the functions in the base class is a virtual function i.e it is intended to have another implementaton in the derived classes. Then it is the need to make the destructor virtual. As if this is not the case then every time even if the Base class pointer is used to create a derived class object .. then also on completion only base classs destructor would be called. For this reason we have a virtual destructor in the base class which allows us to get the corresponding derived class destructor called whose object was maded through base class pointer
Virtual destructor is used when you declare a base class pointer to derived class, and if you try to delete the pointer, it doesnt execute the derived destructor step. Destructor in base class is then made virtual to actually let the derived class destructor function call, and so it can free those variables in the derived class.
- Anonymous September 10, 2008