1 #include<iostream> 2 using namespace std; 3 class A{ 4 public: 5 class B{ 6 public: 7 B(char *name){cout<<"Constructing B:"<<name<<endl;} 8 private: 9 char *name; 10 }; 11 //B如今是类A空间的一个数据类型 12 B b; 13 A():b("In class A"){ 14 cout<<"Constructing A"<<endl; 15 //cout<<name<<endl; 访问不到 16 } 17 }; 18 int main(){ 19 A a; 20 A::B b("Outside class A"); 21 } 22 /* 23 输出: 24 Constructing B:In class A 25 Constructing A 26 Constructing B:Outside class A 27 28 外围类与嵌套类是两个彻底独立的类,并无其余特殊的关系,也就是说 29 嵌套类的成员和外围类的成员没有任何关系,它们不可以互相访问,也 30 不存在友元的关系。 31 32 */
二、局部类ios
1 #include<iostream> 2 using namespace std; 3 void func(){ 4 static int s; 5 class A{ 6 public: 7 int num; 8 void init(int i){ s = i;} 9 }; 10 A a; 11 a.init(8); 12 cout<<s<<endl; 13 } 14 int main(){ 15 func(); 16 } 17 /* 18 一、局部类只能在定义它的函数内部使用,其余地方不能使用 19 二、局部类的全部成员函数都必须定义在类体内。 20 三、局部类的成员函数,除了能够访问成员函数本身的局部变量, 21 类本身的成员变量、全局变量、全局静态变量,还能够访问定义 22 局部类函数的静态变量。 23 四、局部类中不能定义静态数据成员。 24 25 */