Callback to a member function

676 views Asked by At

I have a callback with this signature (I'm using Live555):

typedef void(RTSPClient::responseHandler)(RTSPClient*, ...); //... = other parameters

Then in my code I have created a subclass of RTSPClient following the library rules:

class MyRTSPClient: public RTSPClient { 
... 

//callback 

void continueAfterDESCRIBE(RTSPClient *client, ...); //same signature of typedef

...

};

Now comes the problem.

In my code I have to invoke the following method:

unsigned RTSPClient::sendDescribeCommand(responseHandler, ...); //response handler is the typedef

If I create an object:

MyRTSPClient *client = MyRTSPClient::createNew(...); //is library requirement such creation

how can I pass the function object to sendDescribeCommand as callback?

Of course if I declare continueAfterDESCRIBE as static member I haven't any problem, but I want an object because I have many threads and if I use a static callback called from them many problems of synchronization will be raised.

So I'm struggling (as a newbie in C++) how to find out the correct signature to pass an obj->method as a callback.

2

There are 2 answers

2
Sergey Kalinichenko On BEST ANSWER

You cannot use a non-static member function as a callback parameter that expects a regular function, because their signatures are incompatible:

  • A non-member or a static member function takes only the parameters in its signature
  • A non-static member function takes an additional "hidden" parameter for the object on which it is called.

The common workaround for this is possible only if the library that performs a callback lets you pass custom parameters with the callback registration. This is usually done with a void* pointer, which you pass to the library when you register the callback, and then the library passes it back to you when it calls back the callback function.

Here is how:

// This is the static function that you register for your callback
static void staticContinueAfterDESCRIBE(RTSPClient *client, ...) {
    static_cast<MyRTSPClient*>(client)-> continueAfterDESCRIBE(client, ...);
}

Now that the function is static, you have no problem registering it as a callback. Once the function gets the control, it casts client to your MyRTSPClient* class, and performs the non-static callback.

0
Paul Evans On

You want two pointers, one to an object and one to the member function:

void (RTSPClient::*mfptr) = &RTSPClient::continueAfterDESCRIBE;

and one to an object:

RTSPClient* p = new RTSPClient();

and then call it:

p->*mfptr(...);