如 ISO C++ 2003 中所述
§11.8 Nested classes [class.access.nest]
The members of a nested class have no special access to members of an enclosing class, nor to classes or functions that have granted friendship to an enclosing class; the usual access rules (clause 11) shall be obeyed. The members of an enclosing class have no special access to members of a nested class; the usual access rules (clause 11) shall be obeyed.
[Example:
class E { int x; class B { }; class I { B b; // error: E::B is private ERROR 1 int y; void f(E* p, int i) { p->x = i; // error: E::x is private ERROR 2 } }; int g(I* p) { //return p->y; // error: I::y is private ERROR 3 } }; int main() {}
—end example]
所以我认为 clang和 g++是错误的,因为他们成功地编译了这段代码。
还是我理解有误?
最佳答案
标准说“没有特殊访问权限”,但没有说“完全没有访问权限”。嵌套类与任何其他成员一样是外部类的成员。
C++03标准中没有明确说明,但C++11明确包含:
11.7 Nested classes [class.access.nest]
1 A nested class is a member and as such has the same access rights as any other member.
关于c++ - 根据 ISO 2003,内部类可以访问外部的私有(private)成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30006002/