下面是一个 C++ 代码:
#include<iostream>
using namespace std;
class Base {
public:
virtual int f() const { cout << "Base::f()\n"; }
virtual void f(string) const {}
virtual void g() const {}
};
class Derived4 : public Base {
public:
int f(int) const { cout << "Derived4::f()\n"; }
};
int main() {
string s ("hello");
Derived4 d4;
Base *br = &d4; //line 5
//br->f(1);//line 6
br->f();//line 7
br->f(s);//line 8
}
代码工作正常,但第 6 行出错。该代码正在调用 f() 的基本版本。我还读到如果你在派生类中重新定义函数,该函数的所有基类定义都隐藏了?我错了吗?