4

When using a using-declaration to expose base class' methods, how can I go about exposing methods with the same name but different parameters?

class Base
{
protected:
    void f();
    void f(int);
};

class Derived: public Base
{
    using Base::f; // which is exposed, and how can I manually specify?
};
invertedPanda
  • 142
  • 1
  • 7

1 Answers1

1

In this way, all method in the base class will be exposed and if you want to use just a specific method in derived class you need to use forwarding function

class Base{
  protected:
  void f();
  void f(int);
};

class Derived: public Base
{
 public:
  void f()    //forwarding function
  {
    Base::f();
  }
};

for more explanation of this method you can read Scott Meyers's first book, an Item dedicated to Avoid‬‬ ‫‪hiding‬‬ ‫‪inherited‬‬ ‫‪names‬‬(link to this item)

Saeed Masoomi
  • 1,703
  • 1
  • 19
  • 33
  • This seems like a very hacky solution, though I guess the feeling is very common with the C++ language. Would you know if this actually causes a seperate function to be emitted? Does it cost any more than if I were to otherwise just specify another name for the base-class method? – invertedPanda Sep 21 '19 at 09:09
  • @invertedPanda, the function will be implicitly inlined, so this will cause no any more cost on your program I guess – Saeed Masoomi Sep 21 '19 at 09:12