I am trying to pass a pointer to memeber of class(Dialog::handler) from its method(in scope of Dialog::render) to some outer method(Button::OnClick).
Here is a small example:
class Button
{
public:
void OnClick(void (*handler)())
{
handler();
}
};
class Dialog
{
public:
void handler()
{
//do stuff
}
void render()
{
auto button = new Button;
//Source of problem
button->OnClick(this->*handler);
}
};
But compiler shows error:
non-standard syntax; use '&' to create a pointer to member
Also I triend other combinations, like:
- this->handler.
- &this.handler.
- this.*handler.
- etc
But obviously they failed.
You could use
std::functionand pass it a lambda in which you've caughtthisof the object you'd like to call back:But it looks like you should probably inherit from a common base class that has a
virtual void handler()so you can pass object pointers/references around instead. A rough idea: