C++ Function Callbacks: Cannot convert from a member function to a function signature

后端 未结 6 524
盖世英雄少女心
盖世英雄少女心 2020-11-29 12:22

I\'m using a 3rd party library that allows me to register callbacks for certain events. The register function looks something like this. It uses the Callback signature.

6条回答
  •  情书的邮戳
    2020-11-29 13:17

    You're trying to pass a member function pointer as a normal function pointer which won't work. Member functions have to have the this pointer as one of the hidden parameters, which isn't the case for normal functions, so their types are incompatible.

    You can:

    1. Change the type of your argument to accept member functions and also accept an instance to be the invoking object
    2. Quit trying to pass a member function and pass a normal function (perhaps by making the function static)
    3. Have a normal function that takes an instance of your class, a member function pointer, and a std::string and use something like boost's bind to bind the first two arguments
    4. Make the callback registration function accept a functor object, or an std::function (I think that's the name)
    5. Numerous other ways which I won't detail here, but you get the drift.

提交回复
热议问题