嵌套类的嵌套类的访问权限

时间:2022-01-28 23:01:10

In C++ a nested class has access rights to all members of the enclosing class. Does this also apply to a nested class of a nested class?

在c++中,嵌套类具有对封闭类的所有成员的访问权限。这也适用于嵌套类的嵌套类吗?

This code

这段代码

#include <iostream>

class A
{
public:
    class B
    {
    public:
        B() { std::cout << A::x << std::endl; }

        class C
        {
        public:
            C() { std::cout << A::x << std::endl; }

        };

    };

private:
    static const int x { 0 };

};

int main()
{
    A::B b;

    A::B::C c;
}

compiles without warning on g++ 7.2. However, it is unclear to me that this conforms to the standard. The standard draft (N4727 14.7) says:

在g++ 7.2上进行无警告的编译。然而,我不清楚这是否符合标准。标准草案(N4727 14.7)规定:

A nested class is a member and as such has the same access rights as any other member.

嵌套类是成员,与其他成员具有相同的访问权限。

However, in the example above C is not a member of A, it is a member of a member. Is the standard ambiguous here? Is g++ behavior portable?

然而,在上面的示例中,C不是a的成员,而是成员的成员。这里的标准模棱两可吗?便携式g++行为吗?

2 个解决方案

#1


5  

However, in the example above C is not a member of A, it is a member of a member.

然而,在上面的示例中,C不是a的成员,而是成员的成员。

Yes this is well-defined behavior; the access right is transfered from B.

是的,这是定义良好的行为;使用权由B转让。

According to the standard [class.access]/2,

根据标准[class.access]/2,

A member of a class can also access all the names to which the class has access.

类的成员还可以访问类访问的所有名称。

And [class.mem]/1,

和[class.mem]/ 1,

Members of a class are data members, member functions, nested types, enumerators, and member templates and specializations thereof.

类的成员是数据成员、成员函数、嵌套类型、枚举器和成员模板及其专门化。

C is a nested class of B, it's also the member of B, then C can access names what B could access to, including A::x. For the same reason, C::C is the member of C, it could access names what C could access to, so accessing A::x in C::C is fine.

C是B的嵌套类,也是B的成员,那么C就可以访问B可以访问的名称,包括a::x。出于同样的原因,C::C是C的成员,它可以访问C可以访问的名称,所以在C::C中访问A::x是可以的。

#2


2  

The behavior is well-defined and in-line with the standard wording. What you are missing is the relevant wording of [class.access]p2, which strengthens that which you have already quoted:

行为定义明确,与标准措辞一致。你缺的是[类]的相关措词。[access]p2,它加强了你已经引用的:

A member of a class can also access all the names to which the class has access. A local class of a member function may access the same names that the member function itself may access.

类的成员也可以访问类访问的所有名称。成员函数的本地类可以访问成员函数本身可以访问的名称。

This means that the accessibility is transitive. If C has access to the same entities as B, it also means that C has access to the entities in A, as B has access to them.

这意味着可访问性是传递性的。如果C可以访问与B相同的实体,这也意味着C可以访问A中的实体,因为B可以访问它们。

class A {
  class B {
    class C {
      C() { A::x; /* well-defined */ }
    };
  };

  static int x;
};

#1


5  

However, in the example above C is not a member of A, it is a member of a member.

然而,在上面的示例中,C不是a的成员,而是成员的成员。

Yes this is well-defined behavior; the access right is transfered from B.

是的,这是定义良好的行为;使用权由B转让。

According to the standard [class.access]/2,

根据标准[class.access]/2,

A member of a class can also access all the names to which the class has access.

类的成员还可以访问类访问的所有名称。

And [class.mem]/1,

和[class.mem]/ 1,

Members of a class are data members, member functions, nested types, enumerators, and member templates and specializations thereof.

类的成员是数据成员、成员函数、嵌套类型、枚举器和成员模板及其专门化。

C is a nested class of B, it's also the member of B, then C can access names what B could access to, including A::x. For the same reason, C::C is the member of C, it could access names what C could access to, so accessing A::x in C::C is fine.

C是B的嵌套类,也是B的成员,那么C就可以访问B可以访问的名称,包括a::x。出于同样的原因,C::C是C的成员,它可以访问C可以访问的名称,所以在C::C中访问A::x是可以的。

#2


2  

The behavior is well-defined and in-line with the standard wording. What you are missing is the relevant wording of [class.access]p2, which strengthens that which you have already quoted:

行为定义明确,与标准措辞一致。你缺的是[类]的相关措词。[access]p2,它加强了你已经引用的:

A member of a class can also access all the names to which the class has access. A local class of a member function may access the same names that the member function itself may access.

类的成员也可以访问类访问的所有名称。成员函数的本地类可以访问成员函数本身可以访问的名称。

This means that the accessibility is transitive. If C has access to the same entities as B, it also means that C has access to the entities in A, as B has access to them.

这意味着可访问性是传递性的。如果C可以访问与B相同的实体,这也意味着C可以访问A中的实体,因为B可以访问它们。

class A {
  class B {
    class C {
      C() { A::x; /* well-defined */ }
    };
  };

  static int x;
};