C++ Blog

Tuesday, April 03, 2007

Explicitly calling destructor?

If the object is created on the stack then destructor will be called but when the object goes out of scope the destructor is called again.With the object on heap(created with new operator) the destructor will be executed but the memory won't be released.
So explicitly call the destructor for a object only when that object is created with the "placement" new operator, because here you don't want to release the memory but still you want to execute destructor to release all the resources holded by it.

How to call a destructor?

To call the destructor manually use the following syntax
ClassA ObjA;
ObjA.~A();

The following are incorrect
~ObjA(); // This might call complement operator for an object
~ClassA(); // This creates temporary object and applies complement to it

0 Comments:

Post a Comment

<< Home