#include<iostream>
using namespace std;
class Composition{
public:
//同时有类内初始值和初始化列表时候,最终值为初始化列表中的
Composition():im(this),m(20){cout<<"Composition ctor
";}
private:
int m=9;//类内初始值
//内部定义类
//可访问私有成员
class Inside{
public:
Inside(Composition* cp){
cout<<"Inside ctor
";
cp->m++; //visit private member
cout<<cp->m<<endl; //21
}
};
Inside im;
};
class Composition2{
public:
Composition2():im(this),m(20){cout<<"Composition2 ctor
";}
private:
class Inside2{
public:
Inside2(Composition2* cp){
cout<<"Inside2 ctor
";
cp->m++; //visit private member
cout<<cp->m<<endl; //random number,成员初始化顺序导致
}
};
Inside2 im;
int m=9;
};
int main(){
Composition cp;
//Inside ctor
//21
//Composition ctor
Composition2 cp2;
//Inside2 ctor
//1
//Composition2 ctor
return 0;
}