我有一个继承了许多子类的基类.我需要为抽象方法定义一个新签名,它主要是一个包装器.我试过这个
class B {
public:
virtual void f() = 0;
void f(string msg) { /* print msg and call f() */ }
};
class D : public B {
public:
void f() override { /* implementatation */}
};
int main() {
D d;
d.f("msg");
}
Run Code Online (Sandbox Code Playgroud)
但它不编译并给出以下错误
error: no matching function for call to 'D::f(string)
Run Code Online (Sandbox Code Playgroud)
我的问题是:
D::f(string)无法解决?f(string)为f2(string)(丑陋)D::f(string x) { B::f(x)}(丑陋,因为它必须在每个子类中定义)B::f()(不可接受)更好的解决方案?