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