class A
{
private:
int a;
int b;
int c;
public:
A() : b(2), a(1), c (3)
{
}
};
As per C++ standard data members are constructed and initialized in the order they are declared, correct?
But when using initalization list, we are changing the order of data members, now do they initialize in order of initialization list or the order of declaration?
In the order of declaration, the order in the initialization list does not matter. Some compilers will actually give you warning (gcc) telling you that the initialization list order is different than the order of declaration. This is why you also have to be careful when you would use members to initialize other members, etc.
No, the initialization list has nothing to do with it.
Members are always initialized in the order in which they appear in the class body.
Some compilers will even warn you if the orders are different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With