指定面向具有相同名称的不同函数的类内 using 声明

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

本文关键字:函数 声明 using      更新时间:2023-10-16

当使用using声明公开基类的方法时,如何公开具有相同名称但不同参数的方法?

class Base
{
protected:
void f();
void f(int);
};
class Derived: public Base
{
using Base::f; // which is exposed, and how can I manually specify?
};

这样,基类中的所有方法都将公开,如果您只想在派生类中使用特定方法,则需要使用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)