Virtual destructor
A virtual destructor is a destructor whose dynamic dispatch ensures that destroying an object through a base-class interface invokes the derived-class destructor.
100 C++ Mistakes and How to Avoid Them
A virtual destructor is a destructor declared virtual so destruction through a base-class interface dispatches to the derived object's destructor.
- How it is used
- Protects cleanup when polymorphic objects are deleted through base-class pointers or stored in containers.
- How deep it goes
- Explains the rule, demonstrates undefined behavior and leakage, and contrasts failing and corrected destruction output.
- Examples
- Uses Shape and Square resource cleanup plus Person and Employee objects in polymorphic containers.
- What you can do after
- Diagnose/debug leaks from base-pointer deletion and correct polymorphic base classes with virtual destructors.
Read how 100 C++ Mistakes and How to Avoid Them explains virtual destructor →
Learn C++ by Example
A virtual destructor is a destructor declared virtual so that deleting an object through a base-class pointer invokes the appropriate derived-class destructor.
- How it is used
- Enables safe cleanup when polymorphic objects are accessed and deleted through base-class pointers.
- How deep it goes
- Explains destructor dispatch, undefined behavior, and v-table placement, then stops short of detailed implementation mechanics.
- Examples
- Uses a Blob and DerivedBlob race hierarchy to contrast base-pointer deletion with derived-pointer deletion.
- What you can do after
- Apply it by declaring a defaulted virtual destructor in a polymorphic base class before deleting derived objects through base pointers.
Related concepts
- undefined behavioraddresses · Undefined behavior is program execution for which the applicable language or system specification imposes no requirements on the resulting behavior or effects.
- memory leakaddresses · A memory leak is the unintended retention of allocated memory that a program no longer needs, preventing that memory from being reclaimed for reuse.
- superclassused by · A superclass is a class from which another class inherits members, structure, state, or behavior.
- destructorkind of · A destructor is an operation associated with an object or value that performs cleanup when its lifetime ends.
- polymorphic array elementcontrasts with · A polymorphic array element is an array element accessed through a base-class array interface even though the stored elements have a derived class type.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

