c++ - 根据 ISO 2003,内部类可以访问外部的私有(private)成员

标签 c++ language-lawyer nested-class c++03

如 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]

所以我认为 clangg++是错误的,因为他们成功地编译了这段代码。

还是我理解有误?

最佳答案

标准说“没有特殊访问权限”,但没有说“完全没有访问权限”。嵌套类与任何其他成员一样是外部类的成员。

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/

相关文章:

c++ - C:声明后初始化结构变量

c# - Unity3D C# 随机整数生成器

c++ - 为什么 `const` 指向函数的指针在常量表达式中不可用?

c - scanf ("%d%d", &x, &x) 是否定义明确?

ruby-on-rails - 使用对象值的回形针路径/url

class - 静态内部类是个好主意还是糟糕的设计?

Java:从嵌套类访问主类?

c++ - for(;;) 和 while(1) 有什么区别?

c++ - 返回值是模板类型时如何使用std::result_of?

c++ - 访问不活跃的 union 成员和未定义的行为?