9.8 When Constructors and Destructors Are Called

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.89.10, the global and static objects can alter the order in which destructors are called.

9.8.1 Constructors and Destructors for Objects in Global Scope

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.)

9.8.2 Constructors and Destructors for Non-static Local Objects

The 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.

9.8.3 Constructors and Destructors for static Local Objects

The 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.

9.8.4 Demonstrating When Constructors and Destructors Are Called

The program of Figs. 9.89.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.

Fig. 9.8 CreateAndDestroy class definition.

Alternate View

 1   // Fig. 9.8: CreateAndDestroy.h
 2   // CreateAndDestroy class definition.
 3   // Member functions defined in CreateAndDestroy.cpp.
 4   #include <string>
 5
 6   #ifndef CREATE_H
 7   #define CREATE_H
 8
 9   class CreateAndDestroy {
10   public:
11      CreateAndDestroy(int, std::string); // constructor
12      ~CreateAndDestroy(); // destructor
13   private:
14      int objectID; // ID number for object
15      sdt::string message; // message describing object
16   };
17
18   #endif

Fig. 9.9 CreateAndDestroy class member-function definitions.

Alternate View

 1   // Fig. 9.9: CreateAndDestroy.cpp
 2   // CreateAndDestroy class member-function definitions.
 3   #include <iostream>
 4   #include "CreateAndDestroy.h"// include CreateAndDestroy class definition
 5   using namespace std;
 6
 7   // constructor sets object's ID number and descriptive message
 8   CreateAndDestroy::CreateAndDestroy(int ID, string messageString)
 9      : objectID{ID}, message{messageString} {
10      cout << "Object " << objectID << "   constructor runs   "
11         << message << endl;
12   }
13
14   // destructor
15   CreateAndDestroy::~CreateAndDestroy() {
16      // output newline for certain objects; helps readability
17      cout << (objectID == 1 || objectID == 6 ? "
" : "");
18
19      cout << "Object " << objectID << " destructor runs "
20         << message << endl;
21   }

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.

Fig. 9.10 Order in which constructors and destructors are called.

Alternate View

 1   // Fig. 9.10: fig09_10.cpp
 2   // Order in which constructors and
 3   // destructors are called.
 4   #include <iostream>
 5   #include "CreateAndDestroy.h" // include CreateAndDestroy class definition
 6   using namespace std;
 7
 8   void create(); // prototype
 9
10   CreateAndDestroy first{1, "(global before main)"}; // global object
11
12   int main() {
13      cout << "
MAIN FUNCTION: EXECUTION BEGINS" << endl;
14      CreateAndDestroy second{2, "(local in main)"};             
15      static CreateAndDestroy third{3, "(local static in main)"};
16
17      create(); // call function to create objects
18
19      cout << "
MAIN FUNCTION: EXECUTION RESUMES" << endl;
20      CreateAndDestroy fourth{4, "(local in main)"};
21      cout << "
MAIN FUNCTION: EXECUTION ENDS" << endl;
22   }
23
24   // function to create objects
25   void create() {
26      cout << "
CREATE FUNCTION: EXECUTION BEGINS" << endl;
27      CreateAndDestroy fifth{5, "(local in create)"};              
28      static CreateAndDestroy sixth{6, "(local static in create)"};
29      CreateAndDestroy seventh{7, "(local in create)"};            
30      cout << "
CREATE FUNCTION: EXECUTION ENDS" << endl;
31   }

Object 1   constructor runs   (global before main)

MAIN FUNCTION: EXECUTION BEGINS
Object 2   constructor runs   (local in main)
Object 3   constructor runs   (local static in main)

CREATE FUNCTION: EXECUTION BEGINS
Object 5   constructor runs   (local in create)
Object 6   constructor runs   (local static in create)
Object 7   constructor runs   (local in create)

CREATE FUNCTION: EXECUTION ENDS
Object 7   destructor runs    (local in create)
Object 5   destructor runs    (local in create)

MAIN FUNCTION: EXECUTION RESUMES
Object 4   constructor runs   (local in main)

MAIN FUNCTION: EXECUTION ENDS
Object 4   destructor runs    (local in main)
Object 2   destructor runs    (local in main)

Object 6   destructor runs    (local static in create)
Object 3   destructor runs    (local static in main)

Object 1   destructor runs    (global before main)

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset