I'm using a static class called websocket
which listens on a websocket (hence the name) and calls a callback function once data is received. I created the following declarations
typedef int (*cb)(Json::Value &json);
static void websocket::init();
static void websocket::listen(cb callback, const char* address);
I also have a class called app
which should contain the callback as a member function, because it is using a member property connecting to the database:
int app::callback(Json::Value& json)
{
this->db->insert(json);
};
I'm calling the websocket::listen()
function inside app
like this:
void app::start()
{
websocket::listen(std::mem_fn(&app::callback), this->path);
}
I get an error saying
error: cannot convert 'std::_Mem_fn<int (app::*)(Json::Value&)>' to 'cb' {aka 'int (*)(Json::Value&)'}
. To be able to keep the code as generic as possible so that I can call websocket::listen();
with other callbacks as well, I don't want to change the cb typedef.
I can also not make app::callback
static as it requires this->db
. What is the best way to deal with this problem?