【问题标题】:Overloading a base class function in the derived class在派生类中重载基类函数
【发布时间】:2015-04-04 21:20:07
【问题描述】:

为什么如果派生类中重载了基类函数,则无法通过派生类的对象访问该函数的基类版本(即使是公共的)?

例如:

#include <iostream>
#include <string>

using namespace std;

class Base {
public:
    void f(int i) {
        cout << "\nInteger: " << i << endl;
    }
};

class Derived : public Base {
public:
    void f(string s) {
        cout << "\nString: " << s << endl;
    }
};


int main() {
    Base b;
    Derived d;
    //d.f(5);  Doesn't work
    d.f("Hello");
    //d.Base::f(5); works though
    return 0;
}

【问题讨论】:

  • This 可能是相关的

标签: c++


【解决方案1】:

名称查找在重载决议之前执行。名称查找在一个范围内开始,然后如果它没有找到该名称的声明,它会搜索一个封闭的范围,依此类推,直到找到该名称。在这种情况下,d.f 会找到声明 void Derived::f(string)。只有在Derived 中没有声明成员f 时,名称查找才会继续搜索基类。只有找到名称后,编译器才会确定是否存在适当的重载,如果存在,则哪个重载是最佳匹配。

请注意,您可以在派生类中重新声明基类函数,以强制找到它:

class Derived : public Base {
public:
    using Base::f;
    void f(string s) {
        cout << "\nString: " << s << endl;
    }
};

现在名称查找将找到f 的两个重载,然后重载解析将确定调用哪一个。

【讨论】:

    【解决方案2】:

    如果你想在Derived::f之外还可以Base::f,可以加一行

    using B::f;
    

    Derived.

    class Derived : public Base {
       public:
          using Base::f;
          void f(string s) {
             cout << "\nString: " << s << endl;
          }
    };
    

    现在你可以使用了:

    Derived d;
    d.f(10);
    d.f("Hello");
    

    【讨论】:

      猜你喜欢
      • 2021-03-19
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 2011-03-13
      • 1970-01-01
      相关资源
      最近更新 更多