This section contains Destructor topics.
Name |
Description |
The destructor for a class is called to free members of an object before the object is itself destroyed. The destructor is a member function whose name is that of the class preceded by a tilde (~). A destructor cannot accept any parameters, nor will it have a return type or value declared. | |
When you call exit from within a program, destructors are not called for any local variables in the current scope. Global variables are destroyed in their normal order. | |
A destructor is called implicitly when a variable goes out of its declared scope. Destructors for local variables are called when the block they are declared in is no longer active. In the case of global variables, destructors are called as part of the exit procedure after the main function. When pointers to objects go out of scope, a destructor is not implicitly called. This means that the delete operator must be called to destroy such an object. Destructors are called in the exact opposite order from which their corresponding constructors were called (see Order of calling constructors). | |
A destructor can be declared as virtual. This allows a pointer to a base class object to call the correct destructor in the event that the pointer actually refers to a derived class object. The destructor of a class derived from a class with a virtual destructor is itself virtual. | |
If you call abort anywhere in a program, no destructors are called, not even for variables with a global scope. A destructor can also be invoked explicitly in one of two ways: indirectly through a call to delete, or directly by using the destructor’s fully qualified name. You can use delete to destroy objects that have been allocated using new. Explicit calls to the destructor are necessary only for objects allocated a specific address through calls to new | |
All global objects are active until the code in all exit procedures has executed. Local variables, including those declared in the main function, are destroyed as they go out of scope. The order of execution at the end of a program is as follows:
|
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|