8 Constructors and destructors

Constructors and destructors are invoked the same way in multiple inheritance as in single inheritance. Listing 7 is an example.


Listing 7:constructor
 
1class A 
2{ 
3  public
4    A(void{} 
5    ˜A(void{} 
6}
7 
8class B 
9{ 
10  public
11    B(void{} 
12    ˜B(void{} 
13}
14 
15class C : public A, public B 
16{ 
17  public
18    C(void{} 
19    ˜C(void{} 
20}
21 
22void test(void
23{ 
24  C myC; 
25}

When myC is created as an auto local variable, the constructors A::A() and B::B() are called first, then C::C() is called.

When myC is destroyed when the subroutine returns, the destructor C::~C() is called first, then A::~A() and B::~B() are called.

Note that the order of the calling of the superclasses’ constructors and destructors should not matter, as each super class only handles its own part without visibility of the other superclass.