C++: Warning: Derived::f(char) hides Base::f(double)
(quoted from http://www.parashift.com/c++-faq-lite/strange-inheritance.html)
What's the meaning of, Warning: Derived::f(char) hides Base::f(double) ? Updated!
It means you're going to die.
Here's the mess you're in: if Base declares a member function
public:
void f(double x); ← doesn't matter whether or not this is virtual
};
class Derived : public Base {
public:
void f(char c); ← doesn't matter whether or not this is virtual
};
int main()
{
Derived* d = new Derived();
Base* b = d;
b->f(65.3); ← okay: passes 65.3 to
d->f(65.3); ← bizarre: converts 65.3 to a char (
return 0;
}
Here's how you get out of the mess: Derived must have a using declaration of the hidden member function. For example,
public:
void f(double x);
};
class Derived : public Base {
public:
using Base::f; ← This un-hides
void f(char c);
};
If the using syntax isn't supported by your compiler, redefine the hidden Base member function(s), even if they are non-virtual. Normally this re-definition merely calls the hidden Base member function using the
public:
void f(double x) { Base::f(x); } ← The redefinition merely calls
void f(char c);
};
Note: the hiding problem also occurs if class Base declares a method
Note: warnings are not part of the standard, so your compiler may or may not give the above warning.
Comments