- Constructors and destructors; Called implicitly by compiler
- Order of function calls
- Depends on order of execution; When execution enters and exits scope of objects
- Generally, destructor calls reverse order of constructor calls
- Order of constructor, destructor function calls
- Global scope objects
- Constructors; Before any other function (including main)
- Destructors
- When main terminates (or exit function called)
- Not called if program terminates with abort
- Automatic local objects
- Constructors
- When objects defined; Each time execution enters scope
- Destructors
- When objects leave scope; Execution exits block in which object defined
- Not called if program ends with exit or abort
- static local objects
- Constructors
- Exactly once
- When execution reaches point where object defined
- Destructors
- When main terminates or exit function called
- Not called if program ends with abort
The program of Figs. 1.22-1.25 demonstrates the order in which constructors and destructors are called for objects of class CreateAndDestroy of various storage classes in several scopes.
Figure 1.22:
CreateAndDestroy class definition.
|
Figure 1.23:
CreateAndDestroy class member-function definitions.
|
Figure 1.24:
Order in which constructors and destructors are called. (part 1 of 2)
|
Figure 1.25:
Order in which constructors and destructors are called. (part 2 of 2)
|
2004-07-29