7

我低于警告。我的部分代码是:

class Base {
public:
    virtual void process(int x) {;};
    virtual void process(int a,float b) {;};
protected:
    int pd;
    float pb;
};

class derived: public Base{
public:
    void process(int a,float b);
}

void derived::process(int a,float b){
    pd=a;
    pb=b;
    ....
}

我收到以下警告:

 Warning: overloaded virtual function "Base::process" is only partially overridden in class "derived"

我以任何方式将进程作为虚拟功能,所以我期待这个警告不应该出现......这背后的原因是什么?

4

5 回答 5

10

警告的原因

Warning: overloaded virtual function "Base::process" is only partially overridden in class "derived"

是你没有覆盖所有签名,你已经做到了

virtual void process(int a,float b) {;}

但不是为了

virtual void process(int x) {;}

此外,当您不覆盖并且不使用using Base::process将函数带入静态调用范围时,derived::process(int)甚至不会编译。这是因为 Derivedprocess(int)在这种情况下没有。所以

Derived *pd = new Derived();
pd->process(0);

Derived d;
d.process(0);

不会编译。

添加using声明将解决此问题,以便通过指向 Derived* 的指针对隐藏函数进行静态调用,并选择运算符 d.process(int) 进行编译和虚拟调度(通过基指针或引用调用派生)在没有警告的情况下进行编译。

class Base {
public:
    virtual void process(int x) {qDebug() << "Base::p1 ";};
    virtual void process(int a,float b) {qDebug() << "Base::p2 ";}
protected:
    int pd;
    float pb;
};

class derived: public Base{
public:
    using Base::process;

    /* now you can override 0 functions, 1 of them, or both
    *  base version will be called for all process(s) 
    *  you haven't overloaded
    */
    void process(int x) {qDebug() << "Der::p1 ";}
    void process(int a,float b) {qDebug() << "Der::p2 ";}
};

现在:

int main(int argc, char *argv[])
{
    derived d;
    Base& bref = d;
    bref.process(1);    // Der::p1
    bref.process(1,2);  // Der::p2 
    return 0;
}
于 2014-01-30T17:33:41.523 回答
4

当您覆盖类中的虚拟方法时,该方法的任何未覆盖的重载都对该类隐藏并且无法使用。因此,在您的示例中,尝试调用对象会失败,因为被覆盖process(int)的对象已将其隐藏。derivedprocess(int, float)

于 2014-01-30T17:15:56.553 回答
1

您只覆盖了process. 您错过了仅采用int.

class Base {
public:
    virtual void process(int x) {;}; // You do *not* override this in derived
    virtual void process(int a,float b) {;}; // You do override this
// ...
};

根据您的需要,您可以:

  1. 只需在;中覆盖int重载 derived或者

  2. 使int重载非虚拟并让它调用虚拟int, float重载。

两个旁注:(a)尽管大多数编译器都接受它,但;函数体之后的 a 在语法上是错误的。(b) 受保护的成员变量通常与公共变量一样不受欢迎;您应该使用受保护的 getter/setter 并将变量设为私有。

于 2014-01-30T17:13:22.210 回答
1

当您声明一个与 in 同名的方法时Base,这些方法将被隐藏。

当您覆盖一种方法时就是这种情况。

所以

derived d;
d.process(42); // won't compile

要解决这个问题:添加using Base::process

class derived: public Base {
public:
    using Base::process;
    void process(int a, float b);
};

由于以前的方法不会使 lint 警告静音,解决此问题的另一种方法是覆盖每个方法process

class derived: public Base {
public:
    void process(x) { Base::process(x); }
    void process(int a, float b);
};
于 2014-01-30T17:21:47.457 回答
0

C++ 重载决议

长话短说,在尝试解析名称时,部分覆盖重载函数可能会很奇怪。

另外,从设计的角度来看。图案通常很奇怪。我有一个我认为相同的功能足以保证相同的名称:一般来说,它做同样的事情是一种默契。当你在子类中改变一个函数的行为时,如果你只在派生类中改变它的一部分,那就很奇怪了。本质上,它易于阅读(使用 ~= 大致相同)

//隐式陈述

1) Base::process(int) ~= Base::process(int,float)

2) 派生::进程(int) ~= 派生::进程(int,float)

//明确的陈述

3) Base::process(int) == 派生::process(int)

4) Base::process(int,float) != 派生::process(int,float)

本质上,因为 3 和 4 是冲突的,所以 2 不可能是真的。

于 2014-01-30T17:22:16.993 回答