继承和方法重载 [英] Inheritance and method overloading
本文介绍了继承和方法重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么C ++编译器会出现这个错误?为什么我可以访问lol()从B,但不能访问rofl()[无参数]。
Why C++ compiler gives this error? Why i can access lol() from B, but can not access rofl() [without parameters]. Where is the catch?
class A
{
public:
void lol(void) {}
void rofl(void) { return rofl(0);}
virtual void rofl(int x) {}
};
class B : public A
{
public:
virtual void rofl(int x) {}
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
a.lol();
a.rofl(1);
a.rofl();
B b;
b.lol();
b.rofl(1);
b.rofl(); //ERROR -> B::rofl function does not take 0 arguments
return 0;
}
推荐答案
B :: rofl(int)
隐藏 A :: rofl()
。为了使 A
的 rofl
重载,您应该声明 B
为使用A :: rofl;
。
The B::rofl(int)
'hides' the A::rofl()
. In order to have A
's rofl
overloads, you should declare B
to be using A::rofl;
.
class B : public A {
public:
using A::rofl;
...
};
这是C ++的明智之举:它警告您可能还需要覆盖 B
中的c $ c> A :: rofl()方法。或者你明确声明你使用 A
的其他重载。
This is a wise move of C++: it warns you that you probably also need to override the A::rofl()
method in B
. Either you do that, or you explicitly declare that you use A
's other overloads.
这篇关于继承和方法重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文