Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependencies in Initialization Lists

Tags:

Is this behavior well-defined?

class Foo
{
    int A, B;

    public:

    Foo(int Bar): B(Bar), A(B + 123)
    {
    }
};

int main()
{
    Foo MyFoo(0);
    return 0;
}
like image 826
Maxpm Avatar asked Jun 08 '11 23:06

Maxpm


People also ask

What does an initializer list do?

Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class.

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.

Is initialization list faster?

Conclusion: All other things being equal, your code will run faster if you use initialization lists rather than assignment. Note: There is no performance difference if the type of x_ is some built-in/intrinsic type, such as int or char* or float .

Does Java have initialization list?

As for why Java does not have initializer lists like C++, I would assume that the reason is because all fields are already initialized by default in Java and also because Java uses the super keyword to call the super(or base in C++ lingo)-class constructor.


2 Answers

No, it's undefined. A will be initialized first (it's first in the class definition), and it uses uninitialized B.

Class members are initialized in the order they appear in the class definition, irrespective of their order in the initialization list. Indeed, it is bad practice to mismatch the member definition order with the initialization list order.

If your instance of Foo happened to have static duration, like in Foo f(0); int main(){}, the behavior is well-defined. Objects with static duration are zero-initialized before any other initialization takes place; in that case, A and B will be 0 when the constructor is run. After that, though, the behavior is the same: first A then B, giving A a value of 123 and B a value of Bar (still ugly).

like image 56
GManNickG Avatar answered Sep 24 '22 22:09

GManNickG


No, initialization order is defined by the declaration order in the class itself.

From the C++ standard 12.6.2 [class.base.init] p5:

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. ]

like image 35
Xeo Avatar answered Sep 21 '22 22:09

Xeo