Search code examples
c++c++11inheritancederived-classabstract-base-class

Acessing overloaded function from an Abstract Base class


I know this question has been asked before in some form. But I am still confused.

Suppose I have two classes.

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

class B : public class A{
void foo(int a);
}

Now if I want to make the visibility of the non virtual function in class A public in class B .... how do I do that?? ... In other words currently I am able to call the non virtual function in this manner

B b;
b.A::foo(3, 5);

and I want to avoid this ^

and the solution of putting this

using A::foo; 

in public B refers the virtual function in A .... not the non-virtual function ... so I think that is not the solution.


Solution

  • class A{
    public:
      void foo(int a, int b);
    protected:
      virtual void foo(int a) = 0;
    };
    
    class B : public A{
    public:
      using A::foo;
    protected:
      void foo(int a);
    };
    
    void f()
    {
      B b;
      b.foo(1,2);  // OK
      b.foo(3);    //  error: ‘virtual void B::foo(int)’ is protected within this context
    }
    

    works as expcted. foo with two parameters is callable, foo with one not.