受保护的数据成员和数据函数
当我在类中声明受保护的数据成员时,这意味着它不能被外部世界访问,只能被派生类访问。我的问题是
从派生类派生的类可以访问它吗?
when i declare a protected data member in a class that means its not accesible to the outer world but the derived class. My question is
will it be accesible to a class that is derived from the derived class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,受保护的数据成员可以沿着继承层次结构一路访问。
通常最好避免受保护的数据。另一种方法是编写访问私有数据的受保护方法。这将数据封装在单个类中。它还可以轻松地为数据更改设置断点。
Yes, protected data members are accessible all the way down the inheritance hierarchy.
Protected data is usually better avoided. An alternative is to write protected methods that access the private data. This keeps the data encapsulated within a single class. It also makes it easy to set a breakpoint for changes to the data.
是的。 (顺便说一句,你可以简单地尝试一下。)
Yes. (You could have simply tried that out, by the way.)