Function pointer as parameter

后端 未结 3 1750
心在旅途
心在旅途 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:17

    Replace void *disconnectFunc; with void (*disconnectFunc)(); to declare function pointer type variable. Or even better use a typedef:

    typedef void (*func_t)(); // pointer to function with no args and void return
    ...
    func_t fptr; // variable of pointer to function
    ...
    void D::setDisconnectFunc( func_t func )
    {
        fptr = func;
    }
    
    void D::disconnected()
    {
        fptr();
        connected = false;
    }

提交回复
热议问题