geeksforgeeks-077-c++中的嵌套类

嵌套类指的是声明在一个类里面的类。
类的成员没有特殊权限去访问嵌套类里面的成员。
例如,程序1编译成功,程序2编译失败。
Program 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>

using namespace std;


class Enclosing {

int x;

/* start of Nested class declaration */
class Nested {
int y;
void (Enclosing *e) {
cout<<e->x; // works fine: nested class can access
// private members of Enclosing class
}
}; // declaration Nested class ends here
}; // declaration Enclosing class ends here

int main()
{

}

Program 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>

using namespace std;


class Enclosing {

int x;

/* start of Nested class declaration */
class Nested {
int y;
}; // declaration Nested class ends here

void EnclosingFun(Nested *n) {
cout<<n->y; // Compiler Error: y is private in Nested
}
}; // declaration Enclosing class ends here

int main()
{

}