I have two classes, say A and B; I want to store a function pointer of B in a wrapper function<void()>
in A.
The ultimate goal is to be able to call OnRender()
at multiple places in A, but OnRender()
is defined at runtime (say I want to choose one B among many).
How could this be done ?
class A
{
public:
function<void()> OnRender{}; // I want to use this
void (B::*OnRender)(){}; // I want to avoid this
}
class B
{
public:
auto SomeJob() -> void;
}
I could store a function pointer like this:
someA.OnRender = &B::SomeJob;
// OK if I use void (B::*OnRender)(){};
But I also want to avoid referencing the class B in A.
ANSWER:
I can store a lambda like this:
someIntanceOfA.OnRender = [&](){ someB->SomeJob(); };
// OK if I use function<void()> OnRender{};
Or with safety measures:
someA.OnRender = [&](){ if(somB != nullptr) someB->SomeJob(); };