2

Can someone help me understand why the overloaded bar function doesn't work in this case?

  class A {
  public:
     void foo(int a) {};
     void bar(int a) {};
     virtual void bar()=0;       
  };

  class B : public A{
  public:
      virtual void bar() override {};
  };

  int main(int argc, char* argv[])
  {
      B b;
      b.foo(2); // fine
      b.bar();  // fine
      b.bar(2); // not fine
  }

2 Answers2

3

The overriding works fine, but the override shadows the base class function. That is, name lookup in known class B finds only the one in B, the lookup stops there. You can either use base class qualification, or a using declaration in B, or (ugly) manual forwarders.

Example:

class B
    : public A
{
public:
    using A::bar;
    virtual void bar() override {};
};
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

This is actually in the ISO C++ FAQ, with code examples

Trick is to add a

   public: 
   using A::bar; 

https://isocpp.org/wiki/faq/strange-inheritance#hiding-rule