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.
You cannot use a non-static member function as a callback parameter that expects a regular function, because their signatures are incompatible:
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:
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 yourMyRTSPClient*
class, and performs the non-static callback.