Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly combine functions from based and derived class to avoid warnings?

Tags:

c++

This question is similar to this, but more complex.

there are interfaces and classes:

class B
{
public:
virtual string getName( int index )
    { return names.size() < index ? names[ index ] : string(); };

protected:
    vector<string> names;
};

template< typename T >
class A : public T
{
public:
    string getName( void ) // warning here
      { return name; };

protected:
    string name;
};

int main(int argc, const char * argv[])
{
    A<B> test;
}

As you see i can't to use using T::getName in A because T may not have the getName() method.

How I can to suppress warning:

'A<B>::getName' hides overloaded virtual function

in this case?

like image 349
kaa Avatar asked Mar 19 '23 22:03

kaa


1 Answers

The compiler issued a warning, not an error. Your code is technically correct C++, and the compiler has no problem understanding it, and building it. It is, however, suspect, which is why the compiler warns you that you might be doing something you didn't really mean to do.

The issue is explained here

You inherit a function with this signature:

string getName( int index )

But you declare a function by the same name, with a different signature:

string getName( void )

This means that you lose access to the function you have inherited. It cannot be invoked from A<T>. If you tried to invoke test.getName (4); you will get an error, because class A<T> no longer recognized the function getName() with that signature. It has been hidden (as the compiler warned).

like image 86
Gil Elad Avatar answered Apr 07 '23 13:04

Gil Elad