且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

子类中的 C++ 基类函数重载

更新时间:2022-03-04 05:32:45

正如你所说,BaseClass 的功能被 SubClass 隐藏了.名称 Func 将在 SubClass 的范围内找到,然后名称查找停止,BaseClass 中的 Func 获胜根本不考虑,甚至更合适.它们根本不是超载".

As you said, the BaseClass's function is hidden by the SubClass's. The name Func will be found at the SubClass's scope, then name lookup stops, Func in BaseClass won't be considered at all, even it's more appropriate. They're not "overloads" at all.

请参阅非限定名称查找.

你可以使用 using 将它们引入到同一个作用域中,使重载起作用.

You can use using to introduce them into the same scope to make overloading works.

class SubClass : public BaseClass {
public:
    using BaseClass::Func;
    ~~~~~~~~~~~~~~~~~~~~~
    void Func(int i) {                        // accepts an int, not a float!
        cout << "SubClass::Func() called!";
    }
};