Interview Question

Why should base destructors be virtual?

Use a virtual base destructor for polymorphic deletion.

💡 Concept ✅ Quick Revision 🧠 C++

Answer

A polymorphic base destructor should be virtual when objects may be deleted through a base pointer. • A virtual destructor dispatches destruction to the complete derived object. • Deleting through a base pointer without the required virtual destructor has undefined behavior. • A base not intended for polymorphic deletion can instead use a protected non-virtual destructor.

💡 C++ Example

struct Base { virtual ~Base() = default; }; struct Derived : Base { ~Derived() override = default; };

⚡ Quick Revision

Use a virtual base destructor for polymorphic deletion.