liveBook · Manning

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.

How 2 Manning books explain it

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
Prevents undefined behavior and skipped derived cleanup when deleting polymorphic objects through base pointers.
How deep it goes
Contrasts failing and corrected destructor listings, and recommends virtual destructors for polymorphic base classes.
Examples
Uses Shape and Square listings to contrast skipped derived cleanup with virtual-destructor cleanup.
What you can do after
Diagnose unsafe polymorphic deletion and add a virtual base destructor so derived cleanup runs.

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.

Read how Learn C++ by Example explains virtual destructor →

What to know first

Related concepts

Questions about virtual destructor

What is 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.

Which Manning books explain virtual destructor?

100 C++ Mistakes and How to Avoid Them and Learn C++ by Example.

What should I know before learning virtual destructor?

Manning books that teach virtual destructor usually introduce reference first.

What is the difference between virtual destructor and polymorphic array element?

A virtual destructor is a destructor whose dynamic dispatch ensures that destroying an object through a base-class interface invokes the derived-class destructor. 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.

What is virtual destructor a kind of?

Virtual destructor is a kind of destructor. A destructor is an operation associated with an object or value that performs cleanup when its lifetime ends.

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.