
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Private Destructor in C++
In C++, a private destructor is a destructor that is declared within a private access specifier, which means that the destructor cannot be accessed or called directly outside the class, which is useful for design patterns where the user wants to control how and when an object will destroy.
Syntax
Here is the following syntax for a private destructor, which is declared in the class like any other destructor, but with the private access specifier.
class MyClass {
private:
// Private destructor
~MyClass() {
}
public:
// Public constructor
MyClass() {
// Constructor code here
}
};
Here we will see what will be the case if the destructors are private in C++. Let us see some example codes to get the idea.
Example 1
This code has a private destructor, but it will not generate any error because no object is created.
#include <iostream> using namespace std; class my_class { private: ~my_class(){ //private destructor } }; int main() { }
Example 2
In this program, this will generate a compilation error, as we are trying to create one object, but the compiler can notice that the destructor is not accessible. So it cannot be destroyed after completing the task.
#include <iostream> using namespace std; class my_class { private: ~my_class() { //private destructor } }; int main() { my_class obj; }
Output
[Error] 'my_class::~my_class()' is private
Example 3
Now if we create one pointer for that class, then it will not generate an error because no actual object is created.
#include <iostream> using namespace std; class my_class { private: ~my_class() { //private destructor } }; int main() { my_class *obj; }
Example 4
If one object is created using the new operator, then also no error will be generated. The compiler thinks that this is the programmer's responsibility to delete the object from memory.
#include <iostream> using namespace std; class my_class { private: ~my_class() { //private destructor } }; int main() { my_class *obj = new my_class; }
Output
Example 5
Now if we add a delete statement to delete the object, it will generate an error for the private destructor.
#include <iostream> using namespace std; class my_class { private: ~my_class() { //private destructor } }; int main() { my_class *obj = new my_class; delete obj; }
Output
[Error] 'my_class::~my_class()' is private
Use Cases for a Private Destructor
- In the Singleton Pattern class, the destructor is often made private to prevent external destruction of the instance.
- In smart pointers or reference counting systems, where destructor is made private to make sure that an object is only destroyed when it is no longer in use.
- In Non-Instantiable Abstract Classes, where the user wants to prevent the direct creation of objects of a class, but still wants to allow derived classes to have a destructor.