c – 警告:重载的虚函数“Base :: process”仅在类“derived”中被部分覆盖

前端之家收集整理的这篇文章主要介绍了c – 警告:重载的虚函数“Base :: process”仅在类“derived”中被部分覆盖前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我低于警告.
我的部分代码是:
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"

我以任何方式将过程作为虚函数,所以我期待这个警告不应该来……
这背后的原因是什么?

@H_403_12@

解决方法

警告的原因
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) {;}

另外,当你不重写并且不使用Base :: process将函数带到作用域时,对derived :: process(int)的静态调用甚至都不会编译.这是因为Derived在这种情况下没有进程(int).所以

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

Derived d;
d.process(0);

不会编译.

使用声明添加解决这个问题,通过指向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;
}
@H_403_12@ @H_403_12@ 原文链接:https://www.f2er.com/c/119613.html

猜你在找的C&C++相关文章