9.7. When Constructors and Destructors Are Called

Constructors and destructors are called implicitly by the compiler. 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.79.9, the storage classes of objects can alter the order in which destructors are called.


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


Fig. 9.7. CreateAndDestroy class definition.


 1   // Fig. 9.8: 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   {                                                                 
11      cout << "Object " << objectID << " constructor runs "          
12         << message << endl;                                         
13   } // end CreateAndDestroy constructor                             
14
15   // destructor                                               
16   CreateAndDestroy::~CreateAndDestroy()                       
17   {                                                           
18      // output newline for certain objects; helps readability 
19      cout << ( objectID == 1 || objectID == 6 ? " " : "" );  
20                                                               
21      cout << "Object " << objectID << "   destructor runs    "
22         << message << endl;                                   
23   } // end ~CreateAndDestroy destructor                       


Fig. 9.8. CreateAndDestroy class member-function definitions.


 1   // Fig. 9.9: fig09_09.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( void ); // prototype
 9
10   CreateAndDestroy first( 1, "(global before main)" ); // global object
11
12   int main()
13   {
14      cout << " MAIN FUNCTION: EXECUTION BEGINS" << endl;
15      CreateAndDestroy second( 2, "(local automatic in main)" );   
16      static CreateAndDestroy third( 3, "(local static in main)" );
17
18      create(); // call function to create objects
19
20      cout << " MAIN FUNCTION: EXECUTION RESUMES" << endl;
21      CreateAndDestroy fourth( 4, "(local automatic in main)" );
22      cout << " MAIN FUNCTION: EXECUTION ENDS" << endl;
23   } // end main
24
25   // function to create objects
26   void create( void )
27   {
28      cout << " CREATE FUNCTION: EXECUTION BEGINS" << endl;
29      CreateAndDestroy fifth( 5, "(local automatic in create)" );    
30      static CreateAndDestroy sixth( 6, "(local static in create)" );
31      CreateAndDestroy seventh( 7, "(local automatic in create)" );  
32      cout << " CREATE FUNCTION: EXECUTION ENDS" << endl;
33   } // end function create


Object 1   constructor runs   (global before main)

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

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

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

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

MAIN FUNCTION: EXECUTION ENDS
Object 4   destructor runs    (local automatic in main)
Object 2   destructor runs    (local automatic 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)


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

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

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