C++ virtual destructor
A virtual destructor is needed when you delete a derived object through a base class pointer.
If a class is meant to be used polymorphically, its destructor should usually be virtual.
Simple rule:
If a class has virtual functions, give it a virtual destructor.
The problem
This code uses a base pointer to hold a derived object:
The pointer type is Animal*.
The real object is Dog.
When delete animal runs, C++ must destroy the full Dog object, not only the
Animal part.
Simple example
Output:
Because Animal has a virtual destructor, deleting through Animal* correctly
runs both destructors.
Common modern usage
Prefer smart pointers instead of raw new and delete.
std::unique_ptr<Animal> owns the object.
When the pointer goes out of scope, it deletes the object automatically.
The base destructor still must be virtual so the derived object is destroyed correctly.
Key point
Use a virtual destructor in a base class when:
- the class has virtual functions
- derived objects may be deleted through a base pointer
- the base class is used as a polymorphic interface