10

Possible Duplicate:
overloaded functions are hidden in derived class

I have Class A and Class B (subclass of A)

Class A has function

virtual void foo(int, int);
virtual void foo(int, int, int);

When I try to do

Class B with function

virtual void foo(int, int);

When I try to call foo(int, int, int) with the class the compiler won't let me because it says

no matching function for foo(int,int,int)
candidate is foo(int,int);
Community
  • 1
  • 1
DogDog
  • 4,820
  • 12
  • 44
  • 66
  • Or another potential dupe is http://stackoverflow.com/questions/4146499/why-does-a-virtual-function-get-hidden. Not sure whether the questioner expects that the functions being virtual is important, but it makes no difference. – Steve Jessop Nov 24 '10 at 20:34

3 Answers3

11

The reason why has to do with the way C++ does name lookup and overload resolution. C++ will start at the expression type and lookup upwards until it finds a member matching the specified name. It will then only consider overloads of the member with that name in the discovered type. Hence in this scenario it only considers foo methods declared in B and hence fails to find the overload.

The easiest remedy is to add using A::foo into class B to force the compiler to consider those overloads as well.

class B : public A {
  using A::foo;
  virtual void foo(int, int);  
};
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
3

The override in class B hides the methods of class A with the same name. (Do you have compiler warnings on? Most compilers warn about hidden virtual methods.)

To avoid this, add

using A::foo;

to class B (in whatever public/protected/private section is appropriate).

aschepler
  • 70,891
  • 9
  • 107
  • 161
1

Use

B b;
b.A::foo(1,2,3);

for example.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140