Private Destructors

Why Private Destructors ???

1. While using Reference Counting Objects

The simple answer – Do n't want anyone to be able to destroy the object.

A reference counting object is, its an object that tracks the number of references to itself, and destroys itself when none of the references point toward it. This is because you don't own the lifetime of the reference. Making it simple, the object may be in use by more than one reference simultaneously. Imagine the situation in which the destructor of this object is public and as one of the refrence is released which innocently calls the destructor which is public and destroys the object. This could result in situations like the object being destroyed and other references still pointing to our dead object. To avoid such situations we make the destructor private and provide alternate function which will be careful enough to invoke the destructor only if the reference count is ZERO.

2. Make sure objects are created only on Heap not Stack

We can achieve this by making destructor private. There by preventing the stack unwinding to happen, which will intern avoid creating variables on stack. Then the user will only be able to create object on heap.

3. When you want to create non inheritable-Final Classes

It could happens that you want your class not to be inherited by any other class i.e., to create a final class. For making a class final all you have to do is make the class’s constructor or destructor private. Since in a class there can be any number of constructors where as it can have only one destructor, it’s easy to keep the single destructor as private and make the class Final.

Links:
http://blogs.msdn.com/larryosterman/archive/2005/07/01/434684.aspx
http://prabhagovind.wordpress.com/2006/12/21/some-thing-about-private-destructors/
http://prashanth-cpp.blogspot.com/2007/01/delete-this.html