I know that in a class
the members are initialized in the order they are listed. Does that apply across grouping of variables into public
and private
etc? My confusion is that I could not figure out if there is preference such as private
members get initialized in the order they have been listed before public
members irrespective of where the private variables listed with respect to the public in a class declaration (I know such bias exist towards the base class members)
The rules for class initialization are spelled out in [class.base.init]/11
In a non-delegating constructor, initialization proceeds in the following order:
First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
8 Finally, the compound-statement of the constructor body is executed.
[ Note: The declaration order is mandated to ensure that base and member subobjects are destroyed in the reverse order of initialization. —end note ]
emphasis mine
So when we look at bullet 3 it specifically states that the member are constructed in the order the appear in the definition. This means that regardless of private
, public
, or how they are listed in the class member initialization list they will be constructed in the order they are declared.
Non-static data members are initialized in the order they were declared.
By compiling the following code you can test this if you have warnings enabled:
// Example program
#include <iostream>
#include <string>
class Test {
private:
int a;
public:
int b;
Test() : a(0), b(0) {}
};
class TestPrivatePriority {
public:
int b;
TestPrivatePriority() : a(0), b(0) {}
private:
int a;
};
class TestPublicPriority {
private:
int a;
public:
int b;
TestPublicPriority() : b(0), a(0) {}
};
int main()
{
Test t;
TestPrivatePriority t1;
TestPublicPriority t2;
return 0;
}
This will produce the following self-explanatory warnings:
In constructor 'TestPrivatePriority::TestPrivatePriority()':
25:9: warning: 'TestPrivatePriority::a' will be initialized after [-Wreorder]
20:9: warning: 'int TestPrivatePriority::b' [-Wreorder]
22:5: warning: when initialized here [-Wreorder]
In constructor 'TestPublicPriority::TestPublicPriority()':
35:9: warning: 'TestPublicPriority::b' will be initialized after [-Wreorder]
32:9: warning: 'int TestPublicPriority::a' [-Wreorder]
37:5: warning: when initialized here [-Wreorder]
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