没有用类似原型隐藏非虚拟方法的警告(G ++ 4.4) [英] No warning for hiding non-virtual methods with similar prototype (G++ 4.4)

查看:149
本文介绍了没有用类似原型隐藏非虚拟方法的警告(G ++ 4.4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的项目中发生(很少发生),在派生类中,基类的非虚方法被具有相同原型的方法隐藏。在这种情况下,编译器(在我们的例子中为g ++ 4.4)保持静默。虽然我可以看到没有警告可以用于私有方法,对于保护或公共方法,这应该是至少一个可配置的警告。

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.

如果这样的事情存在我' m无法找到它。

If such a things exists I'm unable to find it.

这里有一个小例子,我想让g ++抱怨(请放心,这种代码模式从来没有这样写一个镜头,通常工作在某个时间点在A中的虚拟方法,并在以后莫名其妙地改变):

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 */ }
};

结果:即使使用-Wall -Wextra也没有警告。

Result: no warning even with -Wall -Wextra.

推荐答案

你不会覆盖方法,你隐藏它。这是一个C ++功能。

You're not overriding the method, you're hiding it. It's a C++ feature.

您可以查看此链接

此外,一个有趣的摘录:

Also, an interesting extract:


注意:警告不是标准的一部分,因此您的编译器可能或
不能给出上述警告。

Note: warnings are not part of the standard, so your compiler may or may not give the above warning.

这篇关于没有用类似原型隐藏非虚拟方法的警告(G ++ 4.4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆