I want to implement an object-oriented function pointer in C++ (comparable to delegates in C#).
I wrote an example code which uses "MagicFunctionPainter" as a placeholder for the final class:
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, ...
}
};
Does std::function or Boost::Signals2 support that? Are there any other librarys that support that case?
Thank you in advance!
The type of
p
should be:Create an object of this type in
B::init
with: