【发布时间】:2020-06-20 00:47:29
【问题描述】:
我有一个类,它的方法有很多签名(在下面的示例中,为了简单起见,只有两个)。
class A
{
public:
virtual void f(int x) const
{
std::cout << "A::f(" << x << ")\n";
}
virtual void f(int x, int y) const
{
std::cout << "A::f(" << x << "," << y << ")\n";
}
};
还有一个派生类,它覆盖了父类的方法,在调用父类方法之前和之后添加了一些逻辑。
class B : A
{
public:
void f(int x) const override
{
std::cout << "B::f(" << x << ")\n";
// do some stuff before the call
A::f(x);
// do some stuff after the call
}
void f(int x, int y) const override
{
std::cout << "B::f(" << x << "," << y << ")\n";
// do some stuff before the call
A::f(x, y);
// do some stuff after the call
}
};
由于调用父方法之前和之后的逻辑在所有重写的方法中都是一致的,因此我试图将所有逻辑封装在一个函数包装器中,该函数包装器将要调用的方法作为参数。
void wrapper(B const *b, std::function<void() const> &f)
{
std::cout << "wrapper(" << b << "," << &f << ")\n";
// do some stuff before the call
f();
// do some stuff after the call
}
class B : A
{
public:
void f(int x) const override
{
std::cout << "B::f(" << x << ")\n";
A const *p = this;
auto g = std::bind(static_cast<void (A::*)(int) const>(&A::f), p, x);
wrapper(this, g);
}
void f(int x, int y) const override
{
std::cout << "B::f(" << x << "," << y << ")\n";
A const *p = this;
auto g = std::bind(static_cast<void (A::*)(int, int) const>(&A::f), p, x, y);
wrapper(this, g);
}
};
我的代码有(至少)两个问题,
-
std::bind不会生成所需的std::function<void() const> - 如果你在类
B的方法f中调用对象g,程序进入一个无限循环,我猜这是由于多态性,即虚拟表解析为B::f而不是A::f
您对如何修复代码有任何建议,或者您知道实现目标的替代方法吗?
谢谢!
【问题讨论】:
标签: c++ oop functional-programming polymorphism overriding