在C ++中覆盖和重载是怎么回事? [英] What's going on with overriding and overloading here in C++?
问题描述
这不起作用:
class Foo
{
public:
virtual int A(int);
virtual int A(int,int);
};
class Bar : public Foo
{
public:
virtual int A(int);
};
Bar b;
int main()
{
b.A(0,0);
}
似乎通过覆盖 Foo :: A int)与
Bar :: A(int)
我有某种方式隐藏 Foo :: A / code>。如果我添加
Bar :: A(int,int)
的东西工作。
It seems that by overriding Foo::A(int)
with Bar::A(int)
I have somehow hidden Foo::A(int,int)
. If I add a Bar::A(int,int)
things work.
推荐答案
基本上,名称查找在重载解析之前发生,您的派生类中的函数 A
将覆盖基类中的虚函数,但在任何基类中隐藏所有其他具有相同名称的函数。
Essentially, name lookup happens before overload resolution so the function A
in your derived class overrides the virtual function in the base class but hides all other functions with the same name in any base classes.
可能的解决方案包括使用Foo :: A; 指令将一个添加到您的派生类中,以使所有的基类成员调用
A
在派生类中可见,或者对具有不同签名的函数使用不同的名称。
Possible solutions include adding a using Foo::A;
directive into your derived class to make all the base class members called A
visible in the derived class or using different names for functions with different signatures.
请参阅这里。
这篇关于在C ++中覆盖和重载是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!