I have a solid understanding of most OOP theory but the one thing that confuses me a lot is virtual destructors.
I thought that the destructor always gets called no matter what and for every object in the chain.
When are you meant to make them virtual and why?
Virtual destructors are useful when you might potentially delete an instance of a derived class through a pointer to base class:
Here, you’ll notice that I didn’t declare Base’s destructor to be
virtual. Now, let’s have a look at the following snippet:Since Base’s destructor is not
virtualandbis aBase*pointing to aDerivedobject,delete bhas undefined behaviour:In most implementations, the call to the destructor will be resolved like any non-virtual code, meaning that the destructor of the base class will be called but not the one of the derived class, resulting in a resources leak.
To sum up, always make base classes’ destructors
virtualwhen they’re meant to be manipulated polymorphically.If you want to prevent the deletion of an instance through a base class pointer, you can make the base class destructor protected and nonvirtual; by doing so, the compiler won’t let you call
deleteon a base class pointer.You can learn more about virtuality and virtual base class destructor in this article from Herb Sutter.