What is the order of subobject initialization?

前端 未结 3 1709
无人及你
无人及你 2021-01-15 06:10

Let we have an object o of class some type which contains member subobjects so and sso of another class types. Consider the following

3条回答
  •  耶瑟儿~
    2021-01-15 06:44

    §12.6.2/10

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

    • First, and only for the constructor of the most derived class (1.8), 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.

    Therefore: no matter which constructor is selected for the initialization---default or otherwise, explicitly defaulted or user-provided---first virtual base classes are initialized, then direct base classes are initialized (in the order in which they appear in the base-specifier-list, just as it says---and not in the order in which they appear in the ctor-initializer); and finally non-static data member subobjects are initialized in declaration order.

提交回复
热议问题