2

I need to assign a callback function with given parameters, so when the event gets triggered and the callback function is called, it calls the function with given parameters. I've looked at multiple websites and I've been trying to find a solution but no luck. Might be that I'm just so dumb, what can I do?

Here's a bit of what I'm trying to do -

void callbackFunction(int someParameter) {
 // Do something here
}

master.ButtonL1.pressed(callbackFunction(3));
// The code above doesn't work, just there to show what I'm wanting to do

2 Answers 2

3

You can use lambda.

master.ButtonL1.pressed([]{callbackFunction(3);});
Sign up to request clarification or add additional context in comments.

Comments

2

In addtion to using a lambda expression, as suggested in the answer by songyuanyao, you can use the following method.

void realCallbackFunction(int someParameter)
{
  // DO the real work
}

static int parameter = 0;
void callbackFunction()
{
   realCallbackFunction(paramter);
}

...

paramter = 3;
master.ButtonL1.pressed(callbackFunction);

2 Comments

Thanks for the answer. This was originally what I was going to do, but I have to do this to multiple buttons. I feel like this would make the code very bulky.
@Latte, if your compiler supports lambda functions, that's the way to go. I am saying that because I am still using VS 2008 at work :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.