Monday, 9 July 2018

Private Destructors

Whenever we want to control destruction of objects of a class, we make the destructor private. For dynamically created objects, it may happen that you pass a pointer to the object to a function and the function deletes the object.


#include
using namespace std;
 
class Test
{
private:
   ~Test() {}
};
int main()
{
  Test t;
}


The above program fails in compilation. The compiler notices that the local variable ‘t’ cannot be destructed because the destructor is private.




#include
using namespace std;
 
class Test
{
private:
   ~Test() {}
};
int main()
{
   Test *t = new Test;
}
The above program also works fine. When something is created using dynamic memory allocation, it is programmer’s responsibility to delete it.
The below program fails in compilation. When we call delete, desturctor is called.
#include
using namespace std;
 
class Test
{
private:
   ~Test() {}
};
int main()
{
   Test *t = new Test;
   delete t;
}
We noticed in the above programs, when a class has private destructur, only dynamic objects of that class can be created. Following is a way to create classes with private destructors and have a function as friend of the class. The function can only delete the objects.
#include
 
// A class with private destuctor
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;
}








































No comments:

Post a Comment