c++ - 警告 : overloaded virtual function "Base::process" is only partially overridden in class "derived"

标签 c++ overriding virtual-functions name-hiding

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

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"

我已经将进程作为虚函数,所以我希望这个警告不应该出现...... 这背后的原因是什么??

最佳答案

警告的原因

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) 甚至不会编译。这是因为 Derived 在这种情况下没有 process(int)。所以

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

Derived d;
d.process(0);

不会编译。

添加 using 声明将解决这个问题,通过指向 Derived* 的指针静态调用隐藏函数并选择运算符 d.process(int) 进行编译和虚拟分派(dispatch)(通过基指针调用 derived或引用)编译没有警告。

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

关于c++ - 警告 : overloaded virtual function "Base::process" is only partially overridden in class "derived",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21462908/

相关文章:

c++ - 我的静态 map 总是空的

python - 在 Python 中调用基类的类方法

c++ - 关于虚拟方法的问题

java - HashSet 在构造函数中调用可重写的方法

C++虚方法: best way to use base implementation with an addition

c++ - 栅格化二维多边形

c++ - 为什么 lower_bound 和 upper_bound 的谓词版本不一致地传递迭代器值?

C++:游戏引擎的事件系统实现

ruby - 关于 ruby​​ 中重写 + 运算符的问题

scala - 在Scala中,父特征是否可以调用子类中实现的方法?