Private Destructor
-
Hi all, What is the use of declaring a private destructor? Where we are using this? Can anybody tell an example Thanks San
The purpose of the private destructor is to avoid the deletion of the object while it is still referenced
Some things seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
The purpose of the private destructor is to avoid the deletion of the object while it is still referenced
Some things seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
Hi all, What is the use of declaring a private destructor? Where we are using this? Can anybody tell an example Thanks San
and other use could be if you want to stop the creation of the object on stack. Yes, you can not delete these object by using delete. Either you have to assume that you don't want to delete such objects or you have to overload the delete or delete these from any friend function. class Test { public: Test(){} private: ~Test(){} }; int _tmain(int argc, _TCHAR* argv[]) { //Test tmp; // Compilation error; Test *testP = new Test(); // work fine return 0; }
Manish Agarwal manish.k.agarwal @ gmail DOT com
-
and other use could be if you want to stop the creation of the object on stack. Yes, you can not delete these object by using delete. Either you have to assume that you don't want to delete such objects or you have to overload the delete or delete these from any friend function. class Test { public: Test(){} private: ~Test(){} }; int _tmain(int argc, _TCHAR* argv[]) { //Test tmp; // Compilation error; Test *testP = new Test(); // work fine return 0; }
Manish Agarwal manish.k.agarwal @ gmail DOT com
-
for delete we have to create a friend function or we have to assume that we don't want to delete this :)
Manish Agarwal manish.k.agarwal @ gmail DOT com
-
for delete we have to create a friend function or we have to assume that we don't want to delete this :)
Manish Agarwal manish.k.agarwal @ gmail DOT com
HI Manish, How to write Friend Function to call Delete on Private Destructor. Thanks, Krishna.
-
HI Manish, How to write Friend Function to call Delete on Private Destructor. Thanks, Krishna.
here is a minimal example
class A
{
private:
~A(){};
public:
A(){};
friend void fn();
};void fn()
{
A a;
}In this case if you create a object of class
A
on stack in any other function other thanfn()
complier will give you an error C2248:'A::~A
' : cannot access private member declared in class 'A
'Manish Agarwal manish.k.agarwal @ gmail DOT com