标签:lang 初始 lan mes end ++ cpp space ios
#include<iostream>
using namespace std;
class Composition{
public:
//同时有类内初始值和初始化列表时候,最终值为初始化列表中的
Composition():im(this),m(20){cout<<"Composition ctor \n";}
private:
int m=9;//类内初始值
//内部定义类
//可访问私有成员
class Inside{
public:
Inside(Composition* cp){
cout<<"Inside ctor \n";
cp->m++; //visit private member
cout<<cp->m<<endl; //21
}
};
Inside im;
};
class Composition2{
public:
Composition2():im(this),m(20){cout<<"Composition2 ctor \n";}
private:
class Inside2{
public:
Inside2(Composition2* cp){
cout<<"Inside2 ctor \n";
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;
}
标签:lang 初始 lan mes end ++ cpp space ios
原文地址:https://www.cnblogs.com/Searchor/p/13983458.html