#include <iostream>
using namespace std;
class A
{
private:
struct B
{
int a;
};
public:
B m;
};
int main()
{
A s;
s.m.a = 10;
cout << s.m.a << endl;
}
输出
10
说明:虽然B是private,但由于m是类成员,可以访问private,所以s.m.a是OK的
int main()
{
A::B t;
t.a = 5;
cout << t.a << endl;
}
编译不通过
error: ‘struct A::B’ is private
说明:因为B是private,所以外部不能直接访问B,所以需要将B改为public,即可