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

后端 未结 6 489
盖世英雄少女心
盖世英雄少女心 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 12:56

    Due to the inflexible hardcoding of an actual function type for the callback you can't use any of the normal adapting or binding tricks to help you here. The third party library really wants you to pass in a non-member function and there isn't much you can do. You may want to consider why you're trying to pass it the address of a member function, and if your design or use of the library could change.

    One option, if you only need to set a single callback, is to have a static or namespace private function that refers to a singleton instance pointer, and uses that to dispatch upon callback.

    If you need multiple items, then possibly a template would wrap the hackiness (untested code here, just the idea).

    template <int which_callback>
    struct CallbackHolderHack
    {
        static int callback_func(std::string str) { dispatchee_->myCallback(str); }
        static MyStruct* dispatchee_;
    };
    
    template <int which_callback>
    MyStruct* CallbackHolderHack::dispatchee_(0);
    

    And use it:

    CallbackHolderHack<0>::dispatchee_ = new MyStruct;
    registerCallback(&CallbackHolderHack<0>::callback_func);
    
    0 讨论(0)
  • 2020-11-29 12:56

    Depends on how you are using it.... eg a Singlton would be much simplier

    struct MyStruct {
        static MyStruct& Create() { 
            static MyStruct m; return m;
        }
        static int StaticCallBack(std::string str) { 
            return Create().Callback(str)
        }
        private:
        int CallBack(std::string str);
        MyStruct();
    };
    

    Or if you want to have many of these objects you have several choices. You would need a way to route it before the callback is called.

    0 讨论(0)
  • 2020-11-29 12:57
    class ICallBackInterface
    {
    public:
        virtual void FireCallBack( std::string& str ) = 0;
    };
    
    std::set<ICallBackInterface*> CallBackMgr;
    
    class MyStruct : public ICallBackInterface
    {
    public:
        MyStruct()
        {
            CallBackMgr.insert( this );
        }
    
        ~MyStruct()
        {
            CallBackMgr.erase( this );
        }
    
        virtual void FireCallBack( std::string& str )
        {
            std::cout << "MyStruct  called\n";
        }
    };
    
    void FireAllCallBack(std::string& str )
    {
        for ( std::set<ICallBackInterface*>::iterator iter = CallBackMgr.begin();
            iter != CallBackMgr.end();
            ++iter)
        {
            (*iter)->FireCallBack( str );
        }
    }
    

    This is another way to use polymorphism to achieve the same effect

    0 讨论(0)
  • 2020-11-29 13:04

    Since you are using C++, why not make the callback a functor object? Then you can use std::mem_fun.

    Edit: Seems std::mem_fun has been deprecated in the latest C++11 standard. So std::function might be a better solution if you have a new compiler.

    See this SO question for hints about using it.

    0 讨论(0)
  • 2020-11-29 13:12

    Member functions are not convertible to the normal functions for its own good reasons. If your design allows, then make MyStruct::myCallback() a static member method and the code should work fine.

    struct MyStruct {
      ...
      static int myCallback(std::string str);
      ^^^^^^
    };
    
    0 讨论(0)
  • 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.
    0 讨论(0)
提交回复
热议问题