Does it depend on the order of declaration of variables?
Yes, the order in which data items (i.e.: i and j in your class A ) is initialized corresponds to the order in which they are declared, and not to the order as they appear in the constructor list of member initializers.
Constructor member initializer list in class A
A(int val) : i(val), j(i + 1)
says nothing about the order in which these data items are initialized.
The data item j will still be initialized to i if j declared before i (i.e.: int j, i ). In this case, j initialized to i + 1 , but i not initialized at this point, which can lead to j containing garbage.
In GCC, you can get a warning displayed in these cases by providing the -Wreorder parameter, which is already enabled by passing the -Wall parameter.
source share