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. The parent says foo accepts two vector arguments. The child correctly implements it with two string arguments, but they are not used. HOWEVER, some children of a parent use these arguments to always be there.

Is it possible to use overloading to allow foo in this Child class to have no arguments, even if the parent says it should?

Many thanks.

+5
source share
1 answer

Do not specify parameter names:

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

This should resolve the warnings.

- - :

// 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;
}
+18

All Articles