清除 C++ 中的函数隐藏编译器警告
我在编译器中收到“隐藏”警告,因为从其父级继承的类具有相同的名称但不同的参数。
添加一个仅发出警告的函数,表明该函数不执行任何操作(在基类中也是如此,但没有警告),将基类函数的参数和名称与派生类相匹配,从而清除此编译器警告。这对派生类有哪些潜在的连锁反应?
编辑:假设我不希望他们能够使用基类函数。 (不要问)。
I'm getting "hiding" warnings in my compiler because a class inherits from its parent has the same name but different parameters.
Adding a function that merely pushes out a warning to say that this function does nothing (as is true in the base class, but without the warning) that matches the parameters and name of the base class function to the derived class clears this compiler warning. What are the potential knock-on effects of this on the derived class?
EDIT: Assume that I do not wish them to be able to use the base class function. (Don't ask).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在派生类中重新定义名称实际上隐藏了基类中的函数。这就是警告告诉你的! :-)
这是一个警告,因为通常这是一个错误。如果是故意的,那没关系(但非常罕见)。
Redefining the name in the derived class effectively hides the function in the base class. That's what the warning tells you! :-)
It is a warning, because usually this is a mistake. If it is on purpose, that is ok (but very rare).
用户无法在不显式写出
myDerivedObj.Base::foo()
的情况下通过派生实例访问基类函数,而他们不太可能这样做。相反,使您的函数签名匹配,或者更改函数名称。
The inability of your users to access the base class function through a derived instance without explicitly writing out
myDerivedObj.Base::foo()
, which they're not likely to do.Make your function signatures match up, instead, or change the function name.
您需要在派生类中取消隐藏基类函数,如下所示
: 示例:
演示: http://ideone.com/OTBxg
You need to unhide the base class function in the derived class as:
Example:
Demo : http://ideone.com/OTBxg