No warning for hiding non-virtual methods with similar prototype (G++ 4.4)

风流意气都作罢 提交于 2019-12-11 08:43:20

问题


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.


回答1:


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:

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



来源:https://stackoverflow.com/questions/8213847/no-warning-for-hiding-non-virtual-methods-with-similar-prototype-g-4-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!