The order in which base classes are constructed depends on the order in which they appear in the class derivation list. The order in which they appear in the constructor initializer list is irrelevant.
Output as below,
ZooAnimal constructor called!
Bear constructor called!
Endangered constructor called!
Panda constructor called!
~Panda called!
~Endangered called!
~Bear called!
~ZooAnimal called
A Panda object is initialized as follows
ZooAnimal
, the ultimate base class up the hierarchy from Panda
’s first direct base class, Bear
, is initialized first.Bear
, the first direct base class, is initialized next.Endangered
, the second direct base, is initialized next.Panda
, the most derived part, is initialized last.The construction order for an object with a virtual base is slightly modified from the normal order: The virtual base subparts of the object are initialized first, using initializers provided in the constructor for the most derived class. Once the virtual base subparts of the object are constructed, the direct base subparts are constructed in the order in which they appear in the derivation list.
For example, when a Panda object is created:
ZooAnimal
part is constructed first, using the initializers specified in the Panda
constructor initializer list.Bear
part is constructed next.Raccoon
part is constructed next.Endangered
, is constructed next.Panda
part is constructed.Note: Virtual base classes are always constructed prior to nonvirtual base classes regardless of where they appear in the inheritance hierarchy.
class Panda : public Bear,
public Raccoon,
public Endangered
//class Panda : public Endangered, // !!!
// public Bear,
// public Raccoon
Output as below,
ZooAnimal constructor called!
Bear constructor called!
Raccoon constructor called!
Endangered constructor called!
Panda constructor called!
~Panda called!
~Endangered called!
~Raccoon called!
~Bear called!
~ZooAnimal called!
Change the order,
//class Panda : public Bear,
// public Raccoon,
// public Endangered
class Panda : public Endangered, // !!!
public Bear,
public Raccoon
Output as below,
ZooAnimal constructor called!
Endangered constructor called!
Bear constructor called!
Raccoon constructor called!
Panda constructor called!
~Panda called!
~Raccoon called!
~Bear called!
~Endangered called!
~ZooAnimal called!