1

I need to have a pointer to a class method and use it to call this method.

In the snippet bellow I am trying to store a call member pointer in the Button::onKeyDown member in order to call it later. However, I can’t figure out the right syntax.

P.S. I don't want to use templates.

class BaseCallback
{
public:
    virtual void onEvent() { cout << "BaseCallback::onEvent >>" << endl; };
};

typedef void (BaseCallback::*Callback)();

class Button
{
public:
    Callback onKeyDown = nullptr;
};

int main()
{
    BaseCallback cb;
    Button btn;

    btn.onKeyDown = &BaseCallback::onEvent;

    (btn.*onKeyDown)(); // <<= This is wrong. What is the proper way to do it?
    // ... 


The right answer is (cb.*btn.onKeyDown)().

zhekaus
  • 3,126
  • 6
  • 23
  • 46

0 Answers0