I'm trying to implement a c++ function that gets a Lambda callback as a parameter. The thing is, the callback is initiated asynchronously from another function in the same (called) class. I therefore need to store the Lambda in a member variable so that it can be accessed by the asynchronous function that needs to initiate the callback.
I tried all the ways I could think of to declare, set and call the Lambda using a member variable, but the code always crashes either in the assignment or in the call.
Here's a stripped-out version of what I'm trying to do.
Declaring the function:
void function(const std::function<void()>callback);
Calling the function from the main code:
myClass->function([](){cout << "Callback called";});
If I execute callback
from within function
it works fine, but I couldn't find a way to store it in a member variable (e.g. m_callback
) and invoke it from another function of the same class.