gcc Woverloaded-virtual warnings

José picture José · Apr 3, 2012 · Viewed 13k times · Source

The following C++ code i think is correct, but produce some warnings when compiled with "-Woverloaded-virtual", is the warning bogus or there is a real problem with this code?

If that is a bogus warning what can i do to avoid it, define all the exception virtual variants in derived get rids of the warning but maybe is a better solution

G++ command:

   g++ -c -Woverloaded-virtual test.cpp 
test.cpp:22:18: warning: ‘virtual void intermediate::exception(const char*)’ was hidden [-Woverloaded-virtual]
test.cpp:32:18: warning:   by ‘virtual void derived::exception()’ [-Woverloaded-virtual]

C++ code

using namespace std;

class base
{
public:

    virtual void exception() = 0;
    virtual void exception(const char*) = 0;
};

class intermediate : public base
{
public:

    virtual void exception()
    {
    cerr << "unknown exception" << endl;
    }

    virtual void exception(const char* msg)
    {
    cerr << "exception: " << msg << endl;
    }
};

class derived : public intermediate
{
public:

    virtual void exception() 
    { 
        intermediate::exception("derived:unknown exception");
    }
};

Answer

Alok Save picture Alok Save · Apr 3, 2012

The warning means that:
When you are not using dynamic dispatch then your derived class object can only call,

 void exception()     

and it will hide all same named methods of the Base class intermediate.

In order that your derived class object can call all the same named methods in base class intermediate, You need to add the following line to your derived class.

 using intermediate::exception;

Ofcourse, You are in best position to decide if this is a problem or not.