理解(简单?)c++继承

Understanding (simple?) C++ Inheritance

本文关键字:c++ 继承 简单 理解      更新时间:2023-10-16

我有点不明白为什么这个代码片段不能编译。

#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,正如预期的那样。

使用<<p>这是验证strong>叮当声, g++ 和MSVC的 cl

知道是怎么回事吗?

但是,在编译时,只识别C中定义的方法,而看不到potential()(这应该是从B继承的)。

c++不这样工作:因为你在C中实现了一个不同的potential方法(一个同名但参数不同的方法),对于C而言,另一个方法是隐藏的

隐藏是因为c++解析(重载)方法名的方式:当你在类的实例(这里是c)上调用方法potential时,c++会在类中搜索该名称的方法是否存在。如果不是这种情况,它将继续在基类中搜索。它在层次结构中继续向上,直到至少找到一个该名称的方法。

但是在您的例子中,c++不需要搜索太远:该方法已经存在于C中,因此它停止搜索。现在c++尝试匹配方法签名。不幸的是,方法签名不匹配,但此时为时已晚:重载解析失败;c++不搜索其他可能匹配的方法。

有三个解决方案:

  1. using在C:

    导入
    class C : public B {
    public:
        using B::potential;
        int potential(int arg, int arg2) { return 3; }
    };
    
  2. main中的基类实例调用方法:

    C c;
    B& b = c;
    int value = b.potential();
    
  3. 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; }
};

我在这里写了一篇文章,深入地演示了这个问题。