8

I'm facing a problem in C++ :

#include <iostream>

class A
{
protected:
  void some_func(const unsigned int& param1)
  {
    std::cout << "A::some_func(" << param1 << ")" << std::endl;
  }
public:
  virtual ~A() {}
  virtual void some_func(const unsigned int& param1, const char*)
  {
    some_func(param1);
  }
};

class B : public A
{
public:
  virtual ~B() {}
  virtual void some_func(const unsigned int& param1, const char*)
  {
    some_func(param1);
  }
};

int main(int, char**)
{
  A* t = new B();
  t->some_func(21, "some char*");
  return 0;
}

I'm using g++ 4.0.1 and the compilation error :

$ g++ -W -Wall -Werror test.cc
test.cc: In member function ‘virtual void B::some_func(const unsigned int&, const char*)’:
test.cc:24: error: no matching function for call to ‘B::some_func(const unsigned int&)’
test.cc:22: note: candidates are: virtual void B::some_func(const unsigned int&, const char*)

Why do I must specify that the call of some_func(param1) in class B is A::some_func(param1) ? Is it a g++ bug or a random message from g++ to prevent special cases I don't see ?

fedj
  • 3,452
  • 1
  • 22
  • 21

1 Answers1

12

The problem is that in the derived class you are hiding the protected method in the base class. You can do a couple of things, either you fully qualify the protected method in the derived object or else you bring that method into scope with a using directive:

class B : public A
{
protected:
  using A::some_func; // bring A::some_func overloads into B
public:
  virtual ~B() {}
  virtual void some_func(const unsigned int& param1, const char*)
  {
    A::some_func(param1); // or fully qualify the call
  }
};
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • Why am I hiding it ? I'm just overriding the second one. Normally, g++ has to keep the signature of the overloaded init in B's vtable but that's not the case. Why doesn't it keep the signatures of every method ? Why does it loose the overloaded one (I'm just redefining one of them) ? – fedj Sep 27 '09 at 22:40
  • 1
    When you define a method with a name in a derived class, it will hide all other methods with the same name above in the hierarchy. When the compiler finds that you are calling `some_func` through a reference of static type B it will try to match it against all occurrences of `some_func` within B itself and will not try to escalate the hierarchy to find possible matches in base classes. – David Rodríguez - dribeas Sep 27 '09 at 22:42
  • Is it a default g++ behaviour to avoid big vtable sizes or is it the C++ definition and every compiler will make the same error ? Because normally, if it was not an overload in class A (let say another method name), the signature would have been copied in class B vtable. – fedj Sep 27 '09 at 22:48
  • 2
    It's in the C++ symbol lookup rules. All compilers should behave exactly this way. Some even emit a warning. – greyfade Sep 27 '09 at 22:53
  • My bad I saw it (10.2). Anyway, my bad a second time, it was already discussed there : http://stackoverflow.com/questions/411103/function-with-same-name-but-different-signature-in-derived-class – fedj Sep 27 '09 at 22:57
  • @fedj: It does not have anything to do with vtable. The hidden method is not even virtual so it could not affect the vtable size in any way. – David Rodríguez - dribeas Sep 27 '09 at 22:59
  • Note that part of your problem is that in A you have two methods by the same name. Even if both of them were virtual, if you only override one of them, then the other will be inaccessible within B unless you bring the method into scope or fully qualify the name at the place of call. – David Rodríguez - dribeas Sep 27 '09 at 23:02