0

I have the following code:

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

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

class C : public B
{
public:
   virtual void foo( int n ) override
   {
      B::foo( n );
   }
};

I checked that code with different compilers and all of them show the same bug: There is no B::foo(n) function to call inside the C::foo(int) function.

According to my non-so-updated c++ knowledge:

  1. A has two different functions signatures.
  2. B overrides foo(): a pure virtual function of class A. It inherits foo(int) for free. In other words, foo(int) should be a function available to the public interface of B.
  3. The public interface of C should have the foo() function inherited from B and overrides foo(int) function that should be available from the public interface of B. Inside C::foo(int) I would call its overridden method from B, B::foo(int).

What were you expecting? Since B inherits publicly from A, it should have in its public interface foo(int) and from that I would expect to call B::foo(int) inside C::foo(int).

CVO
  • 21
  • 2
  • As instructed in [ask], can you please write a descriptive, non-ambiguous title? For more guidance, see [How do I write a good title?](//meta.stackexchange.com/q/10647/997587) – starball Mar 24 '23 at 06:12
  • I think [this](https://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the?rq=2) is related. Note that both calling `A::foo(n);` and putting `using A::foo;` in the definition of `B` work. Not sure what the exact mechanism is here though. – Nathan Pierson Mar 24 '23 at 06:13

0 Answers0