Constructors and destructors are called implicitly when object are created and when they’re about to be removed from memory, respectively. The order in which these function calls occur depends on the order in which execution enters and leaves the scopes where the objects are instantiated. Generally, destructor calls are made in the reverse order of the corresponding constructor calls, but as we’ll see in Figs. 9.8–9.10, the global and static
objects can alter the order in which destructors are called.
Constructors are called for objects defined in global scope (also called global namespace scope) before any other function (including main
) in that program begins execution (although the order of execution of global object constructors between files is not guaranteed). The corresponding destructors are called when main
terminates. Function exit
forces a program to terminate immediately and does not execute the destructors of local objects. The exit
function often is used to terminate a program when a fatal unrecoverable error occurs. Function abort
performs similarly to function exit
but forces the program to terminate immediately, without allowing programmer-defined cleanup code of any kind to be called. Function abort
is usually used to indicate an abnormal termination of the program. (See Appendix F for more information on functions exit
and abort
.)
static
Local ObjectsThe constructor for a non-static
local object is called when execution reaches the point where that object is defined—the corresponding destructor is called when execution leaves the object’s scope (i.e., the block in which that object is defined has finished executing). Constructors and destructors for non-static
local objects are called each time execution enters and leaves the scope of the object. Destructors are not called for local objects if the program terminates with a call to function exit
or function abort
.
static
Local ObjectsThe constructor for a static
local object is called only once, when execution first reaches the point where the object is defined—the corresponding destructor is called when main
terminates or the program calls function exit
. Global and static
objects are destroyed in the reverse order of their creation. Destructors are not called for static
objects if the program terminates with a call to function abort
.
The program of Figs. 9.8–9.10 demonstrates the order in which constructors and destructors are called for global, local and local static
objects of class CreateAndDestroy
(Fig. 9.8 and Fig. 9.9). Each object of class CreateAndDestroy
contains an integer (objectID
) and a string
(message
) that are used in the program’s output to identify the object (Fig. 9.8, lines 14–15). This mechanical example is purely for pedagogic purposes. For this reason, line 17 of the destructor in Fig. 9.9 determines whether the object being destroyed has an objectID
value 1
or 6
and, if so, outputs a newline character. This line makes the program’s output easier to follow.
Figure 9.10 defines object first
(line 10) in global scope. Its constructor is actually called before any statements in main
execute and its destructor is called at program termination after the destructors for all objects with automatic storage duration have run.
Function main
(lines 12–22) declares three objects. Objects second
(line 14) and fourth
(line 20) are local objects, and object third
(line 15) is a static
local object. The constructor for each of these objects is called when execution reaches the point where that object is declared. The destructors for objects fourth
then second
are called—in the reverse of the order in which their constructors were called—when execution reaches the end of main
. Because object third
is static
, it exists until program termination. The destructor for object third
is called before the destructor for global object first
, but after all other objects are destroyed.
Function create
(lines 25–31) declares three objects—fifth
(line 27) and seventh
(line 29) as local automatic objects, and sixth
(line 28) as a static
local object. The destructors for objects seventh
then fifth
are called—the reverse of the order in which their constructors were called—when create
terminates. Because sixth
is static
, it exists until program termination. The destructor for sixth
is called before the destructors for third
and first
, but after all other objects are destroyed.