简体   繁体   中英

How to assign callback function with parameters

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

您可以使用lambda

master.ButtonL1.pressed([]{callbackFunction(3);});

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM