I want to initialize a bunch of members in-class to keep the source file cleaner. However, the objects take an argument I only receive via constructor and can initialize either in the constructor initialization list or in the constructor via assignment. (The second option would certainly not work.) This is basically the scenario:
In Header
class Foo
{
public:
Foo(Pointer * ptr);
private:
Pointer * ptr;
Member m1{ptr, "SomeText"};
Member m2{ptr, "SomeOtherText"};
}
In CPP
Foo::Foo(Pointer*ptr) :
ptr(ptr)
{
// ...
}
Now the question is: Does the standard say anything about the order of initialization between ptr and m1 / m2? Obviously, this code would only work when ptris initialized before m1 and m2.
This is guaranteed by the standard that non-static data members will be initialized in the order of their declarations in the class definition. How they're initialized (via default member initializer or member initializer list) and the order of these initializers don't matter.
[class.base.init]#13.3
That means, the initialization order will always be
ptr->m1->m2.