Is it possible to unhide only specific overloaded method of Base class

Viewed 86
#include <iostream>

class B
{
    public:
    virtual void f() {std::cout<<"Fi";}
    virtual void f(int) {std::cout<<"GI";}

};
class A : public B
{
    public:
    void f(double) {std::cout<<"HI";}   
    using B::f;//but I want to use only f(), not f(int)
};

int main () {
  A a;
  a.f();
  a.f(10);

  return 0;
}

Is it possible in derived class A unhide only f() overload? So a.f(10); would call A::f(double)

2 Answers

You can replace the using statement with a function that calls the base class method:

void f() { B::f(); }

For what you are attempting, make A use protected or private inheritance instead so B's methods are not public in A. Then A can declare its own methods that call B's methods internally.

#include <iostream>

class B
{
public:
    virtual void f() {std::cout<<"Fi";}
    virtual void f(int) {std::cout<<"GI";}
};

class A : protected B
{
public:
    void f(double) {std::cout<<"HI";}   
    void f() { B::f(); }
};

int main () {
  A a;
  a.f();
  a.f(10);

  return 0;
}

Output:

FiHI

Live Demo

Related