Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sharing class member callback with other class

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading