I have two classes, WebUiCoroutine and NADC338 (Worker). The NADC338 class handles some networking, after which I want it to "post" to the WebUiCoroutine class (which is written in header file). Essentially I want to pass/assign postHandler from WebUiCoroutine.h to postUpdate in NADC338 classdsdsdsdsdsdsd
I tried to pass the callback as seen below, and many other ways :/ This one fails with message '=': function as left operand
It’s loosely based upon this question and many other Stack Overflow posts. I’m sure i have misunderstood/overlooked something essential.
WebUiCoroutine.h
class WebUiCoroutine {
NADC338 nadAPI = NADC338("someIPAdress", port);
public:
WebUiCoroutine() {
nadAPI.register_callback(std::bind(&WebUiCoroutine::postHandler, this, std::placeholders::_1));
}
void postHandler(crow::json::wvalue) {
//call another class member method of WebUiCoroutine
}
}
NADC338.cpp
void NADC338::register_callback(std::function<void(crow::json::wvalue)> callback) {
postUpdate = callback;
}
NADC338.h
class NADC338 {
NADC338();
NADC338(string ip, int port);
public:
void (*postUpdate) (crow::json::wvalue);
void register_callback(std::function<void(crow::json::wvalue)> callback);
}
Hope my problem is understandable and the code not too redacted.
Any input is appreciated
>Solution :
Make PostUpdate a std::function rather than a direct function pointer.
It is not possible to cast an std::function when that std::function carries state (as yours must due to the binding).
Instead, make it:
std::function<void(crow::json::wvalue)> PostUpdate;