Good day!

1) There are:

class A; class B: virtual public A; class C: virtual public A; class D: public B, public C; 

In general, the classic constructors are called as: A B C D.

2) Do this:

 class A; class B: public A; class C: virtual public A; class D: public B, public C; 

As I understand it, this is perceived by the class D: AB (v) AC D. virtual A is transferred to the first place and we get the challenge of the constructors: (v) AACB D.

3) Now we do this:

 class A; class D: public А, virtual public A; 

As I understand it, this is perceived by the class D: A, (v) A. constructors call: (v) A. A. - this is according to the logic of the previous example, and so it is impossible, I can agree with that, well. But let's return to the first classic, as I understand it sees this class D: (v) AB (v) AC D. Let the virtuals go to the first place and be: (v) A (v) ABC D. According to my logic 2- of his example. Such cases, help deal with the sequence of calls to the designer in the virtual inheritance. Describe at least why only one basic constructor is called in the first classic and where it is written in descendants that this is necessary. Thank.

  • Links to the detailed description are welcome - fortunado
  • one
  • we can say that class D must know infu about virtual inheritances in the inheritance chain and he calls them and must know about them inf, for example: to call the constructor we need the constructor. Those. in the first example, class D calls class A. And that’s where the work with class A ends. Class B - no longer calls class A. But if class B had no heirs, would it call class A. The question is, does Class B have a check box that the class A constructor has already been called? yes / no) - fortunado
  • one
    @fortunado: your mental model of the behavior of virtual designers is not quite true. It is not B calls (or it decides whether or not to call) the constructors of its virtual base classes, this is the most derived - A Then constructors of non - virtual classes are invoked as usual recursively. - VladD
  • @fortunado: here's an illustration: < ideone.com/ZdYQE3 >. - VladD

1 answer 1

From the C ++ FAQ :

First of all, the constructors of virtual base classes are called throughout the inheritance tree. Their order corresponds to traversing the inheritance tree in depth from left to right (the more right branch of the tree corresponds to a later appearance in the source).

After all the constructors of the virtual base classes have worked out, the order of calling the constructors is from the base class to the derived one. It is easiest to imagine this: at the very beginning of the constructor of the generated class, the compiler makes an implicit call of the constructors of non - virtual base classes (by the way: many compilers do this in reality). For example, if class D generated from B1 and B2 , first constructor B1 is executed, then constructor B2 , then constructor D This rule applies recursively: for example, if B1 in turn is generated from B1a and B1b , and B2 generated from B2a and B2b , the resulting order of the constructors is B1a , B1b , B1 , B2a , B2b , B2 , D

The order of calling B1 and B2 (or B1a and B1b ) is determined by the order in which these base classes appear in the text of the declaration of the child class, and not in the order in which the initializers are in the initialization list of the child class constructor.