警告:重载虚函数“Base::process"仅在类“派生"中部分覆盖 [英] Warning: overloaded virtual function "Base::process" is only partially overridden in class "derived"

查看:110
本文介绍了警告:重载虚函数“Base::process"仅在类“派生"中部分覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am getting below warning . part of my code is :

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"

无论如何我都将进程作为虚函数,所以我期待这个警告不应该出现......这背后的原因是什么??

any way i have made process as virtual function so I am expecting this warning should not come ... What is the reason behind this ??

推荐答案

警告原因

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

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

is that you haven't overridden all signatures, you have done it for

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

但不是为了

virtual void process(int x) {;}

此外,当您不覆盖且不使用 using Base::process 将函数带入对 derived::process(int) 甚至不会编译.这是因为在这种情况下 Derived 没有 process(int) .所以

Additionally, when you don't override and don't use using Base::process to bring functions to scope the static calls to derived::process(int) won't even compile. This is because Derived has no process(int) at that case. So

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

Derived d;
d.process(0);

不会编译.

添加 using 声明将修复此问题,通过指向 Derived* 的指针静态调用隐藏函数并选择运算符 d.process(int) 进行编译和虚拟分派(通过基指针调用派生)或参考)进行编译,没有警告.

Adding using declaration will fix this enabling for static call to hidden functions through pointer to Derived* and select operator d.process(int) to compile and for virtual dispatch (call to derived through base pointer or reference) to compile with no warnings.

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;
}

这篇关于警告:重载虚函数“Base::process"仅在类“派生"中部分覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆