Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class component order of initialisation

Tags:

c++

standards

class D: A
{
    B obj;
    C obj2;
}

What order of construction here is guaranteed?

I know that D will be constructed after A, B and C, but what I really want to know is whether A is guaranteed to be constructed before B or C, or even whether B is guaranteed to be constructed before C.

I know you can have an explicit initialiser list:

D(): A(), B(), C()
{}

but does that initialiser list determine the order of initialisation?

Also, does whether or not any of the components do or don't have a default constructor?

like image 296
matt Avatar asked Jun 29 '11 02:06

matt


People also ask

In what order are class members attributes initialized?

Initialization is always in the order that the class members appear in your class definition, so in your example a , then b .

What is the order of initialization for data?

Generally, the order of execution is from top to bottom and left to right. But a rare condition arises where this rule fails is when the initializer list is used in class. In the initializer list, the order of execution takes place according to the order of declaration of member variables.

How do you initialize a class in C++?

There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.

Is it mandatory to use constructor in a class in C++?

It is mandatory to have a constructor with the right access modifier. However, the compiler supplies a default if an access modifier is not defined in the class and a constructor is not declared.


1 Answers

From the C++03 standard ISO/IEC 14882:2003(E) §12.6.2/5 [class.base.init]:

Initialization shall proceed in the following order:
— First, and only for the constructor of the most derived class as described below, virtual base classes shall be 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 class names in the derived class base-specifier-list.
— Then, direct base classes shall be initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
— Then, nonstatic data members shall be initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
— Finally, the body of the constructor is executed.
[Note: the declaration order is mandated to ensure that base and member subobjects are destroyed in the reverse order of initialization. ]

So in this case, you are guaranteed that the order of initialization will be first the base class A, then the subobject B (since it appears first in the list of class members in the class definition), then the subobject C. The order of the initializer list is irrelevant, as is whether or not any of the members do or do not have a default constructor—if a member does not have a default constructor and it is not explicitly initialized in an initializer list, then it has an unspecified value.

like image 138
Adam Rosenfield Avatar answered Sep 17 '22 13:09

Adam Rosenfield