- 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. 10-13 demonstrates the order in which constructors and destructors are called for objects of class CreateAndDestroy of various storage classes in several scopes.
Figure 10:
CreateAndDestroy class definition.
|
Figure 11:
CreateAndDestroy class member-function definitions.
|
Figure 12:
Order in which constructors and destructors are called. (part 1 of 2)
|
Figure 13:
Order in which constructors and destructors are called. (part 2 of 2)
|
2004-07-01