What I know:
From a post: C++: Initialization Order of Class Data Members
nonstatic data members shall be initialized in the order they were declared in the class definition
From C++ Primer :
Class definitions are processed in two phases:
• First, the member declarations are compiled.
• Function bodies are compiled only after the entire class has been seen.
Why can I initialize c
before initializing b
?
Btw, are the data member inside class declarations or definitions? I think they are definitions, but the quoted text above seems to indicate they are merely declarations.
Sample code, compiles well:
#include <iostream>
using namespace std;
struct Bar {
void funcA() const {
c++;
}
int &c = b;
int b = 3;
};
int main()
{
Bar b;
b.funcA();
cout << b.b << endl;
}