Explicit Destructor Calls

Calling a destructor explicitly is seldom necessary. However, it can be useful to perform cleanup of objects placed at absolute addresses. These objects are commonly allocated using a user-defined new operator that takes a placement argument. The delete operator cannot deallocate this memory because it is not allocated from the free store (for more information, see “The new and delete Operators”). A call to the destructor, however, can perform appropriate cleanup. To explicitly call the destructor for an object, s, of class String, use one of the following statements:

s.String::~String();

or

ps->String::~String();

The notation for explicit calls to destructors, shown above, can be used regardless of whether the type defines a destructor. This allows the programmer to make such explicit calls without knowing if a destructor is defined for the type. An explicit call to a destructor where none is defined has no effect.

Note:

Explicit calls to destructors call only the destructor for the class specified. When destructors are declared as virtual, the virtual function calling mechanism is disabled for these explicit calls.