Why can a C++ base class need a virtual destructor?
Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Saved automatically
Model Answer & Deep-Dive Explanation
When an object is destroyed through a base pointer, a public virtual destructor permits the derived destructor to run correctly. Deleting a derived object through a base pointer without a suitable virtual destructor is undefined behavior in the ordinary polymorphic case. If deletion through the base is forbidden, a protected nonvirtual destructor can express a different design. Making other methods virtual does not automatically make the destructor virtual. State the intended ownership and destruction path rather than adding virtual dispatch to every type indiscriminately.