Possible Duplicate:
Member fields, order of construction
If i have a class with two members like this:
class A
{
int a;
int b;
A() {}
};
Is the order in which a and b are constructed undefined?
If I use cl, then no matter in which order I call the constructors, the members are always constructed in the order in which they are declared in the class. In this case it would always be a then b, even if I define the constructor for A like:
A() : b(), a() {}
But I am assuming that that is just the behaviour of the specific compiler.
No. Members are constructed in the order in which they are declared.
You are advised to arrange your initializer list in the same order, but you are not required to do so. It’s just very confusing if you don’t and may lead to hard-to-detect errors.
Example:
This construction is actually undefined behaviour, because you’re reading an uninitialized variable in the initializer
a(b).Standard reference (C++11, 12.6.2/10):