ale*_*ale 5 c++ polymorphism pure-virtual
我有以下内容:
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;
}
Run Code Online (Sandbox Code Playgroud)
foo(...)没有Parent实现,因为它是一个纯虚函数.父母说foo有两个向量参数.子进程使用两个字符串参数正确实现它,但它们没有被使用.但是,父母的一些孩子会使用这些论点,所以他们需要永远在那里.
有没有什么方法可以使用重载来允许给定的Child类中的foo没有参数,即使父级说它必须?
非常感谢.
lit*_*adv 18
不要指定参数名称:
// arg1 and arg2 not used - GIVES WARNING
bool Child::foo(vector<string>, vector<string>) {
return false;
}
Run Code Online (Sandbox Code Playgroud)
这应该解决警告.
如果您的编译器出于某种原因不支持它 - 请执行以下操作:
// 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;
}
Run Code Online (Sandbox Code Playgroud)