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

时间:2011-05-05 11:24:43

标签: 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 %i\n", value);
    return 0;
}

我有两个纯虚方法,在抽象超类potential中都命名为A。子类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>

已使用 clang g ++ 和MSVC的 cl 进行验证。

有关正在发生的事情的任何想法?

2 个答案:

答案 0 :(得分:26)

  

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

C ++不能像这样工作:因为你在potential中实现了一个不同的C方法(一个名称相同但参数不同的方法),另一个方法是隐藏< / em>就C而言。

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

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

有三种解决方案:

  1. 在C:

    中使用using导入它
    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();
    

答案 1 :(得分:11)

问题在于名称隐藏。

函数重载和函数继承不是最好的朋友。通常你 [嗯,三个人的“或者”是什么?]

  • 在单个类中重载函数。一切正常。
  • 从基类继承非重载函数。一切正常。
  • 从派生类B中的基类C重新实现非重载函数。 C::func 隐藏 B::func,因为它具有相同的名称。

你正在使用继承重载,而你的C::func正在隐藏B::func 以及它的每次重载,即使是那些在C中重新实施。

这有点古怪的C ++混淆,但它很容易解决。

简而言之,解决方案是使用B::potential()语句将C纳入using的范围:

class C : public B {
public:
    using B::potential; // brings overloads from B into scope
    int potential(int arg, int arg2) { return 3; }
};

我写了一篇文章here,深入论证了这个问题。