0

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*)`
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
Chris Tonkinson
  • 13,823
  • 14
  • 58
  • 90

3 Answers3

9

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
  • 339,232
  • 124
  • 596
  • 636
  • 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 Tonkinson Dec 11 '09 at 18:49
  • 2
    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 Dec 11 '09 at 19:41
2

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
  • 219,715
  • 46
  • 258
  • 445
0

It was already answered at:

Why does an overridden function in the derived class hide other overloads of the base class?

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

Community
  • 1
  • 1
J. Calleja
  • 4,855
  • 2
  • 33
  • 54