views:

150

answers:

3

Perhaps my knowledge of inheritance and polymorphism isn't what I thought it was. Can anyone shed some light?

Setup (trivialization of problem):

class X {
};

class Y {
};

class Base {
  public:
    void f( X* ) {}
};

class Child: public Base {
  public:
    void f( Y* ) {}
};

Question: This should work, right?

int main( void ) {
  X* x = new X();
  Y* y = new Y();
  Child* c = new Child();
  c->f( x );
  c->f( y );
  return 0;
}

I get errors (GCC 4.4) to the tune of:

`no matching function for call to 'Child::f(X*&)'`
`note: candidates are: void Child::f(Y*)`
+8  A: 

The virtual keyword will not help you here.

Your base class Base::f is being hidden by your derived type. You need to do the following:

class Child: public Base {
  public:
    using Base::f;
    void f( Y* ) {}
};

Parashift goes into more detail.

Brian R. Bondy
Okay, I didn't know you could do 'using' in a class like that - thanks.At any rate, I'm well familiar with the concept of name hiding - but is it really **ONLY** based upon function name, and not the entire signature?
Chris
Yes, the look-up by name happens before overload resolution, thus `Base::f` isn't considered for overloads without pulling them in explicitly.
Georg Fritzsche
Okay, that makes sense.
Chris
+1  A: 

Your derived class' f() hides the base class' f(). You can prevent this by explicitly bringing Base::f() into the derived class' scope:

class Child: public Base {
  public:
    using Base::f;
    void f( Y* ) {}
};
sbi
Beat me by some seconds ;)
Georg Fritzsche
A: 

It was already answered at:

http://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the

When you declare a method on a derived class, it hides any method with the same name from the base class.

J. Calleja