What is the exact order in which the calls to the constructors are made during inheritance (virtual inheritance is involved)? [duplicate]

  c++, multiple-inheritance, virtual-inheritance
[EDIT: I went through relevant questions on Stackoverflow, but they weren’t helpful.]

I was thinking about writing an algorithm (a graph traversal algorithm) to determine the order of execution of the constructors in a given class-hierarchy (in C++).

This is how I was modelling the problem:

  1. I am assuming there is only a default constructor in every class.
  2. Every node represents a class, and every node has a list of directed edges.
  3. Every edge points to the parent class. Every edge has a value of 0 or 1: 0 means the inheritance is virtual and 1 means the inheritance is non-virtual.
  4. The list of edges in the node are ordered in the same order as they are declared in the class definition.

To come up with the algorithm, I tried to know the exact order in which the C++ compiler calls the constructors, so I went through this:
Link

After reading it, I couldn’t quite understand what this statement meant:

They are executed in the order they appear in a depth-first left-to-right traversal of the graph of base classes, where left to right refer to the order of appearance of base class names.

To understand it I also tried to read the answers to a similar question on Stackoverflow:
Link

But I wasn’t able to understand how the calls for the constructors are done when cases of virtual inheritance arise.

Can someone provide a better explanation for the order in which the constructors get called?

Source: Windows Questions C++

LEAVE A COMMENT