Reputation: 86155
Where I have this struct,
struct
AAA
{
AAA() : bbb(2)
{
// ccc ???
}
int bbb = 1;
int ccc = bbb;
};
AFAIK, if there's an initialization-list :bbb(2)
, the expression bbb = 1
will be ignored. And then, it's vague to me what ccc
will become finally.
Which one of initialization-list or brace-or-equal initializer would be evaluated first? What's the rule between them?
Upvotes: 7
Views: 1141
Reputation:
The rule was always that fields are always initialised in order of declaration, and C++11 didn't change that. That means bbb
's initialiser runs first, then ccc
's initialiser runs. It doesn't matter whether either initialiser is specified on the field or as part of the constructor.
Upvotes: 11
Reputation: 181077
The C++11 draft §12.6.2.9 says;
If a given non-static data member has both a brace-or-equal-initializer and a mem-initializer, the initialization specified by the mem-initializer is performed, and the non-static data member’s brace-or-equal-initializer is ignored.
[ Example: Given
struct A {
int i = /∗ some integer expression with side effects ∗/ ;
A(int arg) : i(arg) { }
// ...
};
the A(int) constructor will simply initialize i to the value of arg, and the side effects in i’s brace-or- equal-initializer will not take place. — end example ]
Since initialization is done in declaration order (§12.6.2.10) with the addition of this rule, the value of bbb
and ccc
will both be 2.
Upvotes: 12