我想在 C++ 中实现一个面向对象的函数指针(类似于 C# 中的委托)。
我写了一个示例代码,它使用“MagicFunctionPainter”作为最终类的占位符:
class A
{
public:
MagicFunctionPointer p;
void fireEvent()
{
p();
}
};
class B
{
public:
void init(A* a)
{
a->p = &onEvent;
}
void onEvent()
{
// in a real-world app, it'd modify class variables, call other functions, ...
}
};
std::function 或 Boost::Signals2 支持吗?还有其他支持这种情况的库吗?
先感谢您!