Difficulty: Intermediate
Why should the destructor of a polymorphic base class be virtual? What happens if it isn't?
If there is one rule you should be able to recite in your sleep for C++ interviews, it is 'make base class destructors virtual'. Let's understand why, so that it isn't just a rule you memorised.
Consider the typical polymorphic setup: 'Base* p = new Derived();' followed later by 'delete p;'. The compiler sees only a Base pointer. If Base's destructor is not virtual, the call to the destructor is resolved statically, at compile time, against the pointer's type. So only ~Base() runs. Derived's destructor is skipped entirely, and whatever Derived owns (a heap buffer, a file handle, a lock) is never released. Even worse, the C++ standard says that deleting a derived object through a base pointer with a non-virtual destructor is undefined behaviour, not merely a leak. The program might crash, corrupt the heap, or seem to work.
If the destructor is virtual, deleting through a Base pointer performs dynamic dispatch. The most-derived destructor runs first, then destructors of each base class in reverse order of construction, all the way up. That is what you expect from object lifetime: build from the base upward, tear down from the top downward. Think of unpacking Russian nesting dolls: you must start with the outermost doll, but if you only have a handle on the inner doll's label, you need someone to tell you which outer doll to open first. The vtable plays that role.
When do you actually need it? Whenever a class is designed to be a polymorphic base, that is, whenever it has any virtual function or you expect to delete derived objects through base pointers. A handy rule: if a class has a virtual function, give it a virtual destructor. If a class is not designed for inheritance, do not make it virtual, because it adds a vptr to every object and costs a little performance. Standard library types like std::vector, std::string and std::map have non-virtual destructors, so never inherit from them for polymorphic use.
There is a second option when you want a base class that cannot be deleted through its pointer: make the destructor protected and non-virtual. Nobody can call delete on a base pointer (it fails to compile), while derived classes can still be destroyed normally. This is guideline C.35 in the C++ Core Guidelines, along with 'a base class destructor should be either public and virtual, or protected and non-virtual'.
Smart pointers help, but with caveats. std::shared_ptr<Base> p = std::make_shared<Derived>() remembers the deleter for Derived at creation time, so it works correctly even if Base's destructor isn't virtual. std::unique_ptr<Base> does not; it will call ~Base only. Do not rely on this: just make the destructor virtual.
Finally, remember pure virtual destructors need a body, and if you declare a virtual destructor as '= default', you should be aware it suppresses implicit move operations, so declare those explicitly if needed.
#include <iostream>
struct Base {
Base() { std::cout << "Base ctor\n"; }
virtual ~Base() { std::cout << "Base dtor\n"; } // remove 'virtual' -> UB below
};
struct Derived : Base {
int* buf;
Derived() : buf(new int[10]) { std::cout << "Derived ctor\n"; }
~Derived() override { delete[] buf; std::cout << "Derived dtor\n"; }
};
int main() {
Base* p = new Derived;
delete p; // with a virtual destructor: correct full teardown
}
Construction goes base to derived; destruction goes derived to base. Without virtual on ~Base, this delete would be undefined behaviour.
virtual destructor, undefined behaviour, polymorphism, resource leak