'Pass method as other method callback parameter

I'm trying to give a method as a callback of another method just like that:

Actions actions;
Button button;

int main()
{
    actions = Actions();
    button = Button();
    
    button.onClick(actions.doSmthg);

    return 0;
}

Here is my Actions:

class Actions {
  public:
    Actions();
    void doSmthg();
};

and here is the Button with my attempt of implementing a callback pattern:

class Button {
  public:
    Button() {};
    void onClick(void (*callbackPtr)());
};

Sadly I got the following error:

error: invalid use of non-static member function ‘void Actions::doSmthg()’

I've check multiple examples that suggest to use std::bind when dealing with callbacks but I'm really not sure how to make it work.

Any idea to implement such a pattern in C++?

Here is a live sandbox https://onlinegdb.com/nL3SIUOaI.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source