Sequence of constructor calls in multiple inheritance

前端 未结 1 1781
[愿得一人]
[愿得一人] 2021-01-12 20:33

I have tried to find a lot that what if only one class is made virtual in multiple inheritance? The behaviour of constructor call is not clear to me in this

相关标签:
1条回答
  • 2021-01-12 20:53

    The standard says [C++11 section 12.6.2/10] that :

    In a non-delegating constructor, initialization proceeds in the following order:

    — First, and only for the constructor of the most derived class, virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.

    — Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).

    — Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).

    — Finally, the compound-statement of the constructor body is executed.

    So your virtual base classes are always built first... This is really important in the case of virtual base class sharing.

    0 讨论(0)
提交回复
热议问题