Search code examples
c++syntaxusing-declaration

Specifying an in-class using-declaration targeting different functions with the same name


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?
};

Solution

  • 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)