What is the use of having destructor as private?

Basically, any time you want some other class to be responsible for the life cycle of your class’ objects, or you have reason to prevent the destruction of an object, you can make the destructor private.

For instance, if you’re doing some sort of reference counting thing, you can have the object (or manager that has been “friend”ed) responsible for counting the number of references to itself and delete it when the number hits zero. A private dtor would prevent anybody else from deleting it when there were still references to it.

For another instance, what if you have an object that has a manager (or itself) that may destroy it or may decline to destroy it depending on other conditions in the program, such as a database connection being open or a file being written. You could have a “request_delete” method in the class or the manager that will check that condition and it will either delete or decline, and return a status telling you what it did. That’s far more flexible that just calling “delete”.

Leave a Comment