First of all, I'm beginner in C++ so I don't know if the way I'm doing this is right or can cause some problem.
So, I'm trying to pass a generic callback to a function and then call member functions from different objects inside that callback. In this case, I'm creating a closure with a lambda function to capture the objects and then inside the lambda I'm calling they member functions.
Well, for example, the code is something like this:
void functionWithCallback(std::function<void()> callback)
{
// Do some stuff ...
callback();
}
int main()
{
SomeClass *some_object = new SomeClass();
AnotherClass *another_object = new AnotherClass();
std::function<void()> callback = [some_object, another_object]() {
some_object->someFunction();
another_object->anotherFunction();
};
functionWithCallback(callback);
}
My question is: is this a correct way or can it cause some problems or unexpected behaviors? Is there any more elegant way to do this?
Thanks!
Aucun commentaire:
Enregistrer un commentaire