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 */