理解(简单?)c++继承
Understanding (simple?) C++ Inheritance
我有点不明白为什么这个代码片段不能编译。
#include <cstdio>
class A {
public:
virtual int potential()=0;
virtual int potential(int arg, int arg2)=0;
};
class B : public A {
public:
int potential() { return 1; }
virtual int potential(int arg, int arg2) { return 2; }
};
class C : public B {
public:
int potential(int arg, int arg2) { return 3; }
};
int main(int argc, char** argv) {
C c;
int value = c.potential();
printf("Got %in", value);
return 0;
}
我有两个纯虚方法,在抽象超类A
中都命名为potential
。然后子类B
定义了这两个方法,但进一步的子类C
只需要重新定义其中一个方法。
但是,在编译时,只识别C
中定义的方法,而看不到potential()
(这应该是从B
继承的):
In function 'int main(int, char**)':
Line 23: error: no matching function for call to 'C::potential()'
compilation terminated due to -Wfatal-errors.
如果我将A::potential(int, int)
从继承树一直重命名为其他东西,比如A::somethingElse(int, int)
,那么代码就会编译得很好,输出是Got 1
,正如预期的那样。
知道是怎么回事吗?
但是,在编译时,只识别C中定义的方法,而看不到potential()(这应该是从B继承的)。
c++不这样工作:因为你在C
中实现了一个不同的potential
方法(一个同名但参数不同的方法),对于C
而言,另一个方法是隐藏的。
隐藏是因为c++解析(重载)方法名的方式:当你在类的实例(这里是c
)上调用方法potential
时,c++会在类中搜索该名称的方法是否存在。如果不是这种情况,它将继续在基类中搜索。它在层次结构中继续向上,直到至少找到一个该名称的方法。
但是在您的例子中,c++不需要搜索太远:该方法已经存在于C
中,因此它停止搜索。现在c++尝试匹配方法签名。不幸的是,方法签名不匹配,但此时为时已晚:重载解析失败;c++不搜索其他可能匹配的方法。
有三个解决方案:
-
用
导入using
在C:class C : public B { public: using B::potential; int potential(int arg, int arg2) { return 3; } };
-
从
main
中的基类实例调用方法:C c; B& b = c; int value = b.potential();
-
在
main
中明确限定名称:C c; int value = c.B::potential();
问题是名称隐藏
函数重载和函数继承并不是最好的朋友。通常你either[嗯,"either"表示三是什么意思?) :
- 在单个类中重载函数。一切正常。从基类继承非重载函数。一切正常。在派生类
C
中重新实现基类B
的非重载函数。C::func
隐藏了 B::func
,因为它们的名字相同。你正在使用继承和重载,你的C::func
隐藏了B::func
和它的每个重载,甚至那些在C
中没有重新实现的重载。
这是c++中有点奇怪的混乱,但很容易解决。
简而言之,解决方案是使用using
语句将B::potential()
纳入C
的作用域:
class C : public B {
public:
using B::potential; // brings overloads from B into scope
int potential(int arg, int arg2) { return 3; }
};
我在这里写了一篇文章,深入地演示了这个问题。
- 继承函数的重载解析
- 继承期间显示未知行为的子类
- 头文件-继承c++
- 为什么在保护模式下继承升级不起作用
- 通过继承类使用来自不同命名空间的运算符
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 混合组合和继承的C++问题
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 从类继承时,继承的类是否会通过父类重新定义继承的变量
- 公共与私人继承
- 如何创建从同一类继承的不同对象的向量
- 如何从另一个文件继承私有成员变量和公共函数
- 在模板基类中为继承类中的可选重写生成虚拟方法
- 带有继承的C++工厂
- 我应该避免多重实现继承吗
- C++继承更改成员
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 关于C++中具有多重继承"this"指针的说明
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 如何在QT Creator上将QWidget声明为继承类的对象