对于隐藏具有类似原型的非虚拟方法没有警告(G++ 4.4)
在我们的项目中,会发生(很少但确实发生)在派生类中,基类中的非虚拟方法被具有相同原型的方法隐藏。在这种情况下,编译器(在我们的例子中是 g++ 4.4)保持安静。虽然我可以看到没有警告对私有方法有用,但对于受保护或公共方法,这至少应该是一个可配置的警告。
如果存在这样的东西,我无法找到它。
这是一个我想让 g++ 抱怨的小例子(请放心,这种代码模式永远不会一次性写成这样,通常工作在某个时间点是 A 中的虚拟方法,后来莫名其妙地改变了):
class A
{
public:
void work(int p)
{ /* do something */ }
};
class B : public A
{
public:
void work(int p)
{ /* do something different */ }
};
结果:即使使用 -Wall -Wextra 也没有警告。
In our projects it happens (rarely but it happens) that in a derived class a non-virtual method from the base-class is hidden by a method with the same prototype. In that case the compiler (in our case g++ 4.4) stays quiet. While I can see that no warning can be useful for private methods, for protected or public methods this should be at least a configurable warning.
If such a things exists I'm unable to find it.
Here's a small example I'd like to have g++ complain about (be assured that this kind of code-pattern is never written like this in one shot, usually work was at some point in time a virtual method in A and was inexplicably changed later):
class A
{
public:
void work(int p)
{ /* do something */ }
};
class B : public A
{
public:
void work(int p)
{ /* do something different */ }
};
Result: no warning even with -Wall -Wextra.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有重写该方法,而是隐藏了它。这是一个 C++ 功能。
您可以查看此链接。
另外,摘录一段有趣的内容:
You're not overriding the method, you're hiding it. It's a C++ feature.
You can take a look at this link.
Also, an interesting extract: