Information Technology Reference
In-Depth Information
Constructor Execution
In the preceding chapter, you saw that a constructor executes code that prepares a class for
use. This includes initializing both the static and instance members of the class. In this chapter,
you saw that part of a derived class object is an object of the base class.
￿
To create the base class part of an object, a constructor for the base class is called as part
of the process of creating the instance.
￿
Each class in the inheritance hierarchy chain executes its base class constructor before
it executes its own constructor body.
For example, the following code shows a declaration of class MyDerivedClass and its con-
structor. When the constructor is called, it calls the parameterless constructor MyBaseClass()
before executing its own body.
class MyDerivedClass : MyBaseClass
{
MyDerivedClass() // Constructor uses base constructor MyBaseClass().
{
...
}
The order of construction is shown in Figure 7-11. When an instance is being created, one
of the first things that is done is the initialization of all the instance members of the object. After
that, the base class constructor is called. Only then is the body of the constructor of the class
itself executed.
Figure 7-11. Order of object construction
Search WWH ::




Custom Search