No warning for hiding non-virtual methods with sim

2019-08-02 19:17发布

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条回答
Anthone
2楼-- · 2019-08-02 19:37

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.

查看更多
登录 后发表回答