c++ 中基类中受保护字段的问题
我有一个基类,例如 BassClass
,其中包含一些字段(我将其设置为受保护的)和一些纯虚函数。然后是派生类,例如 DerivedClass
,例如 class DerivedClass : public BassClass
。 DerivedClass 不应该继承 BassClass 的受保护字段吗?当我尝试编译 DerivedClass 时,编译器抱怨 DerivedClass 没有任何这些字段,这里出了什么问题? 谢谢
I have a base class, say BassClass
, with some fields, which I made them protected, and some pure virtual functions. Then the derived class, say DerivedClass
, like class DerivedClass : public BassClass
. Shouldn't DerivedClass inherit the protected fields from BassClass? When I tried to compile the DerivedClass, the compiler complains that DerivedClass does NOT have any of those fields, what is wrong here?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
BassClass
(原文如此)和DerivedClass
是模板,并且您想要从DerivedClass
访问的BassClass
成员不是' t 指定为从属名称,它将不可见。例如,
要获得访问权限,您需要提供更多信息。例如,您可以完全指定成员的名称:
或者您可以明确指出您引用的是类成员:
If
BassClass
(sic) andDerivedClass
are templates, and theBassClass
member you want to access fromDerivedClass
isn't specified as a dependent name, it will not be visible.E.g.
To gain access you need to give more information. For example, you might fully specify the member's name:
Or you might make it explicit that you're referring to a class member:
这有效:
This works: