Why do we need a virtual destructor in a base class? What happens if you delete a derived class object through a base class pointer without one?
Last updated
Deleting a derived object through a base pointer without a
virtualdestructor is undefined behavior. Typically only~Base()runs, leaking derived resources.A virtual destructor dispatches through the vtable, so the dynamic type's destructor runs first, then the bases in reverse order.
Give every polymorphic base a virtual destructor. Skip it for non-polymorphic classes to avoid needless vptr overhead.