vendredi 5 mai 2023

Pass function as paramater of another function c++ 11

I have this code in c++11

// To register a callback
void registerCallback(void (*fnPtr)(int state)) {
}

class Test {
public:
    // To pass in the callback
    void helloTest(int state) {   
    }

    void registerTheCallbackHere()
    {
        // This does not pass ( I want to it to pass)
        auto lamda = [this](int state) {
            this->helloTest(state);
        };
        registerCallback(lamda); // error here

        // But this one pass 
        auto lamda2 = [](int state) {
        };
        registerCallback(lamda2); // no error
    }
};

I have a function that a callback function as argument called registerCallback. I have class Test which has a method "helloTest". I want to pass this method to the register call back inside another method of Test class called registerTheCallbackHere using a lamda function but it's not working. Can you help please with an alternative solution.

Aucun commentaire:

Enregistrer un commentaire