brace-or-equal-initializers initialization order

Viewed 215

If using brace-or-equal-initializers, what's the initializing order of member variables? Are they initialize the same as code order?

struct foo {
    int x = 1;
    int y = x + 1;
} bar;

Will bar.y always be 2 regardless of the compiler?

2 Answers

From Scott Meyers book, Effective C++, item 4:

One aspect of C++ that isn’t fickle is the order in which an object’s data is initialized. This order is always the same: base classes are initialized before derived classes (see also Item 12), and within a class, data members are initialized in the order in which they are declared.

Related