How to pass bound class static method as function arg

Viewed 29

Having class FastBot that works only using callbacks, using FastBot::attach(void (*handler)(FB_msg &msg)) Having class ControlTGBot that has non-static private method void _messageCb(FB_msg& msg) that should be passed as callback function.

How to pass _messageCb as a function to FastBot::attach a way that also can also make it possible to work inside _messageCb with ControlTGBot instance?

Variant1 (broken):

class ControlTGBot : IControl
{
private:
    FastBot _bot;
    void _messageCb(FB_msg& msg) {...}
public: 
    void start()
    {
        _bot.attach(_messageCb); // Broken
    }
}

Variant2 (IDK if it is broken or not, typing errors):

class ControlTGBot : IControl
{
private:
    FastBot _bot;
    static void _messageCb(ControlTGBot* self, FB_msg& msg) {...}
public: 
    void start()
    {
        std::function<void(FB_msg&)> fnCallback = 
            std::bind(&ControlTGBot::_messageCb, this, std::placeholders::_1);
        _bot.attach(fnCallback); // no suitable conversion function from "std::function<void (FB_msg &)>" to "void (*)(FB_msg &msg)"
    }
}
0 Answers
Related