【发布时间】:2017-02-02 15:58:40
【问题描述】:
在继承的Granting Access主题中从The Complete Reference学习C++时,写到我们可以恢复类的成员恢复到原来的访问状态。
示例
class Base {
public:
int x;
};
class Derived : private Base {
public:
Base::x; // make x public again
};
据作者介绍
您可以使用访问声明来恢复 public 和 受保护的成员。 但是,您不能使用访问声明来提高或降低成员的访问状态。 例如,在基类中声明为私有的成员不能被派生类公开。 如果 C++ 允许这种情况发生,它将破坏其封装机制。
我不明白这将如何发生?
【问题讨论】:
-
您需要引用完整的上下文。
We can not raise or lower the access status of a member这通常是不正确的。例如,使用受保护或公共继承的类可以将基类的受保护成员公开。 -
我添加了完整的段落@dxiv
-
x已在Base中公开...您还缺少using吗? -
@newbee 你确定这是完整的段落吗?
You can use an access declaration to restore the access rights of public and protected member. However,you can not use an access declaration to raise or lower the access status of a member.除非您将member更改为private member,否则第二句与第一句相矛盾。 -
这很不幸。访问声明已被弃用,本书根本不应该讨论它们,除非在脚注中。考虑再买一本书。
标签: c++