Protected vs Private Destructor

If the base class destructor is private or protected then you cannot call delete through the base-class pointer.

Use a protected destructor to prevent the destruction of a derived object via a base-class pointer. It limits access to the destuctor to derived classes. And it prevents automatic
(stack) objects of class base.

In effect it is used to allow any
other
polymorphic use of derived
classes via pointers to base, but not
allow the users to delete using such a
pointer. Example:- Abstract Base Classes / Interfaces.

But a protected, non-virtual destructor on a non-final class seems to be a bug waiting to happen. Assuming you do not provide a destroy() function, you have to eventually make the dtor public. As soon as you do that, you have no further control over the class, and run the risk of polymorphic deletion with a non-virtual dtor, if someone derives further from your class.

Leave a Comment