我低于警告。 我的部分代码是:
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/