[英]Nested class access to enclosing class members
From C++ Primer 5th edition By Stanley Lippman et al(19.5): 摘自Stanley Lippman等人(19.5)撰写的C ++ Primer 5th Edition:
A nested class can have the same kinds of members as a nonnested class. 嵌套类可以具有与非嵌套类相同的成员。 Just like any other class, a nested class controls access to its own members using access specifiers. 就像其他任何类一样,嵌套类使用访问说明符控制对其自身成员的访问。 The enclosing class has no special access to the members of a nested class, and the nested class has no special access to members of its enclosing class . 封闭类对嵌套类的成员没有特殊的访问权限,而嵌套类对其封闭类的成员没有特殊的访问权限 。
Is there any truth to the bolded part? 粗体部分有什么道理吗? I could find no mention of the nested class being restricted access to enclosing class members in the standard (9.7 N3337) and the following code compiles fine (g++ 5.2.0) 我发现没有提到嵌套类被限制访问标准(9.7 N3337)中的封闭类成员,并且以下代码可以很好地进行编译(g ++ 5.2.0)
#include <iostream>
struct A{
private:
typedef int woah;
public:
struct B{
woah x = 5;
void test() { A f; std::cout << f.x;}
};
private:
int x = 5;
};
int main(){
A::B j;
j.test();
}
There being two parts here: 这里有两个部分:
B
accesses the private type alias woah
to define its own member. B
访问私有类型别名woah
来定义其自己的成员。 test
of B
accesses the private x
member of an A
object. B
的成员函数test
访问A
对象的private x
成员。 Of course the opposite seems to be true as the quote says: A
cannot access private members of B
(not that this example shows that). 当然,引号中的说法似乎恰恰相反: A
无法访问B
私有成员(此示例并未表明这一点)。 So is this a blunder on my book's part or am I misunderstanding what it is saying? 那么这是我书本上的失误还是我误解了它在说什么?
It was decided that the lack of access of nested classes was a mistake in the Standard, and subsequently rectified. 决定缺少嵌套类的访问权限是该标准中的一个错误,并随后得到纠正。 Now nested classes enjoy the same levels of access as all members, namely total access. 现在,嵌套类具有与所有成员相同的访问级别,即总访问权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.