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?
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With