I read about the constructor initialization order in C ++ Super-FAQ from the C ++ programming language website . Here is the following code.
#include <iostream>
class Y {
public:
Y();
void f();
};
Y::Y() { std::cout << "Initializing Y\n"; }
void Y::f() { std::cout << "Using Y\n"; }
class X {
public:
X(Y& y);
};
X::X(Y& y) { y.f(); }
class Z {
public:
Z();
protected:
X x_;
Y y_;
};
Z::Z()
: y_()
, x_(y_)
{ }
int main()
{
Z z;
return 0;
}
The printed sequence of this code is:
Using y
Initialization Y
, , , Z y_ Y x_ X. , , , Y:: f(), Y, std:: cout < " Y\n";.