【发布时间】:2020-03-06 14:46:54
【问题描述】:
我有一些关于继承和函数重载的问题。我写了一些类似下面的接口。现在我正在尝试从派生类调用父类的某些函数,但它没有按我的预期工作。
为什么可以调用b.hello() 而不能调用b.test()?
#include <iostream>
using namespace std;
class A {
public:
void hello() {}
void test() {}
virtual void test(int a) {}
};
class B : public A {
public:
void test(int a) override {}
};
int main() {
B b;
// Is possible to call test(int) through B
b.test(1);
// Is not possble to call test() through B
b.test();
// But, is possible to call hello() through B
b.hello();
}
【问题讨论】:
-
你得到什么错误?你尝试了哪些改变?
-
询问代码和错误时,请显示真实代码(您缺少
;,这让我觉得您的真实代码不同)和错误。 -
因为编译器试图在
B中找到test函数,当它在那里找到它时,发现你试图在没有参数的情况下调用它。
标签: c++ inheritance overloading