Let we have an object o
of class some type which contains member subobjects so
and sso
of another class types. Consider the following
§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.
It is not correct to say that default initialization is performed for object o
. In your example object o
is copy-initialized. Default initialization in your example is performed for the unnamed object created by new
(which subsequently becomes leaked).
Now, your unnamed object of type O
created by new
is indeed default initialized, which in this case means that it is initialized by a call to user-defined default constructor O::O()
. The constructor initializer list in O::O()
constructor is completely absent, i.e. does not mention any of the subobjects. That means that these subobjects will be default initialized.
As stated in
12.6.2 Initializing bases and members
8 In a non-delegating constructor, if a given non-static data member or base class is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer) and the entity is not a virtual base class of an abstract class (10.4), then
— if the entity is a non-static data member that has a brace-or-equal-initializer, the entity is initialized as specified in 8.5;
— otherwise, if the entity is a variant member (9.5), no initialization is performed;
— otherwise, the entity is default-initialized (8.5).
The last option applies to your case. (The numbering might be off since I'm using a draft version of the document.)
Note that the title of your question mentions the "order of subobject initialization", while the actual question has nothing to do with the order per se. It is about the method of initialization.
8.5/6 To default-initialize an object of type T means:
- if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called...