Chapter Sixteen: Introduction to Private Destructors
// This program is an example of a private destructor
#include <iostream>
using namespace std;
class Test {
private:
~Test() {}
};
int main()
{
}
If you run the above code, you will see it compile with no errors. Therefore, you can say that there is no compiler error in the code, which indicates that the compiler does not throw an error when it comes across a private destructor. Consider the program below:
// This program is used to explain how a private destructor functions
#include <iostream>
using namespace std;
class Test {
private:
~Test() {}
};
int main()
{
Test t;
}
When you run the above code, you will receive a compile error. The compiler notes that you have declared a variable âtâ and it cannot delete it from the class or code since the destructor you have defined is private. What do you think happens in the following code?
// Code to understand private destructors
#include <iostream>
using namespace std;
class Test {
private:
~Test() {}
};
int main()
{
Test* t;
}
When you run the above code, you do not receive any error. There is no object constructed as part of the code, and the compiler uses the pointer. Therefore, there is no use of a destructor. Now, what about the following program?
// Example of a private destructor
#include <iostream>
using namespace std;
class Test {
private:
~Test() {}
};
int main()
{
Test* t = new Test;
}
When you run the above code, you will not receive any error in the code. Why do you think this is the case? The above code uses dynamic memory allocation to store the variables. Therefore, it is your duty to delete the object stored in the dynamic memory. It is for this reason why the compiler does not care.
In case you create a destructor and label it as a private member function, you can also create another instance in the class using the malloc() function (memory allocation). Consider the following example:
// Example of a private destructor
#include <bits/stdc++.h>
using namespace std;
class Test {
public:
Test() // Constructor
{
cout << "Constructor called\n";
}
private:
~Test() // Private Destructor
{
cout << "Destructor called\n"
;
}
};
int main()
{
Test* t = (Test*)malloc(sizeof(Test));
return 0;
}
You do not receive any output when you run the code. The following code fails to compile accurately.
// Example of a private destructor
#include <iostream>
using namespace std;
class Test {
private:
~Test() {}
};
int main()
{
Test* t = new Test;
delete t;
}
When you create a class with a private destructor, a dynamic object is created for those classes. The following example is one where you can create a class using a private destructor. You can also create a friend function in the class. This function is only used to delete objects.
// Example of a private destructor
#include <iostream>
// a class with private destructor
class Test {
private:
~Test() {}
friend void destructTest(Test*);
};
// Only this function can destruct objects of Test
void destructTest(Test* ptr)
{
delete ptr;
}
int main()
{
// create an object
Test* ptr = new Test;
// destruct the object
destructTest(ptr);
return 0;
}
When you want to control or monitor the destruction or deletion of objects in a class, you need to make the destructor private. If you use dynamically created objects in your classes, you can delete the object by passing pointers in the function as arguments or parameters. These functions delete the objects in the code. It is
important to note that a reference to an object after the function is called will lead to a dangler.