开发者

Pure virtual functions and unused arguments in child functions in C++

I have the following:

class Parent {
public:
    virtual bool foo(vector<string> arg1, vector<string> arg2) = 0;
};

class Child : public Parent {
public:
    bool foo(vector<string> arg1, vector<string> arg2);
};

// arg1 and arg2 not used - GIVES WARNING
bool Child::foo(vector<string> arg1, vector<string> arg2) {
    return false;
}

There is no Parent implementation of foo(...) because it is a pure virtual function. Th开发者_开发问答e parent says that foo takes two vector arguments. The child implements it correctly with two string arguments but they're not used. HOWEVER, some children of Parent WILL use these arguments so they need to always be there.

Is there any way I can use overloading to allow foo in the given Child class not to have the arguments even though the parent says it has to?

Many thanks.


Don't specify the parameter names:

// arg1 and arg2 not used - GIVES WARNING
bool Child::foo(vector<string>, vector<string>) {
    return false;
}

That should solve the warnings.

If your compiler for whatever reason doesn't support it - do this:

// arg1 and arg2 not used - GIVES WARNING
bool Child::foo(vector<string> arg1, vector<string> arg2) {
    (void)arg1; (void)arg2; // ignore parameters without "unused" warning
    return false;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜