Function pointer as parameter

后端 未结 3 1749
心在旅途
心在旅途 2020-12-07 12:52

I try to call a function which passed as function pointer with no argument, but I can\'t make it work.

void *disconnectFunc;

void D::setDisconnectFunc(void          


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 13:15

    The correct way to do this is:

    typedef void (*callback_function)(void); // type for conciseness
    
    callback_function disconnectFunc; // variable to store function pointer type
    
    void D::setDisconnectFunc(callback_function pFunc)
    {
        disconnectFunc = pFunc; // store
    }
    
    void D::disconnected()
    {
        disconnectFunc(); // call
        connected = false;
    }
    

提交回复
热议问题