Is there any difference between a protected and a private destructor in C++? If a base classes destructor is private, I imagine that it is still called when deleting the derived class object.
-
4You'll have some problems deriving from the class with private destructor =) – SadSido Jul 14 '10 at 13:53
-
Duplicate for http://stackoverflow.com/questions/631783/what-is-the-use-of-having-destructor-as-private ? – SadSido Jul 14 '10 at 14:00
-
1Similar question here: http://stackoverflow.com/questions/224966/private-and-protected-members-c – Jordan Jul 14 '10 at 14:00
4 Answers
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.

- 7,092
- 3
- 36
- 50
-
*But a protected, non-virtual destructor seems to be a bug waiting to happen* seems to contradict to the [Core Guidelines](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c35-a-base-class-destructor-should-be-either-public-and-virtual-or-protected-and-non-virtual). – agentsmith Feb 23 '22 at 10:17
-
Taken from here:
If the constructor/destructor is declared as private, then the class cannot be instantiated.
This is true, however it can be instantiated from another method in the class. Similarly, if the destructor is private
, then the object can only be deleted from inside the class as well. Also, it prevents the class from being inherited (or at least, prevent the inherited class from being instantiated/destroyed at all).

- 76,817
- 74
- 166
- 248
-
5public class A{ private A(){} public A getA(){ return new A(); }} Not exactly true. – Stefan Kendall Jul 14 '10 at 13:57
-
3That's not true. Object with private destructor *can* be instantiated, (on a stack inside a friend function, for example). – SadSido Jul 14 '10 at 13:59
-
3Right can't be stack allocated but can be heap allocated and also from friend functions... – Brian R. Bondy Jul 14 '10 at 13:59
-
3That's also the case if the destructor is protected, so this doesn't give a difference between protected and private. – Mike Seymour Jul 14 '10 at 14:10
-
4Missing fact: You can `new` a class that has a private destructor, but you cannot `delete` it (outside of member functions and friend functions). – Thomas Eding Oct 22 '12 at 17:28
The following piece of code will result in the compiler error (VC2010): C2248: 'base::~base' : cannot access private member declared in class 'base'
class base
{
~base(){}
};
class derived : public base
{
};
int main ()
{
derived* d = new derived;
delete d;
}
However, if you change the base destructor to be protected, everything works fine.

- 24,346
- 3
- 50
- 88
-
2But even if you changed it to `protected`, you couldn't destroy objects through a base class pointer. (In that case it should also be `virtual`, BTW.) Which somewhat defeats many of the purposes of derivation... – sbi Jul 14 '10 at 15:22
The answer is that your assumption is wrong. The base class destructor cannot be called when it is private.

- 54,333
- 19
- 131
- 194
-
-
2@deus-ex-machina399: It cannot. Therefore you cannot derive from it. And you cannot create automatic objects of it. And dynamic objects can never be deleted (unless you provide a member function which does so). – sbi Jul 14 '10 at 15:20