Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用参数分配回调函数_C++ - Fatal编程技术网

C++ 如何使用参数分配回调函数

C++ 如何使用参数分配回调函数,c++,C++,我需要分配一个具有给定参数的回调函数,因此当触发事件并调用回调函数时,它将使用给定参数调用该函数。我看过多个网站,我一直试图找到一个解决方案,但没有运气。也许是因为我太笨了,我能做什么 以下是我想做的一些事情- void callbackFunction(int someParameter) { // Do something here } master.ButtonL1.pressed(callbackFunction(3)); // The code above doesn't work

我需要分配一个具有给定参数的回调函数,因此当触发事件并调用回调函数时,它将使用给定参数调用该函数。我看过多个网站,我一直试图找到一个解决方案,但没有运气。也许是因为我太笨了,我能做什么

以下是我想做的一些事情-

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表达式之外,如中所建议,还可以使用以下方法

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

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

...

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

谢谢你的回答。这本来是我要做的,但我必须对多个按钮执行此操作。我觉得这会使代码变得非常庞大。@Latte,如果你的编译器支持lambda函数,那就是最好的方法。我这么说是因为我仍然在工作中使用VS2008:)
void realCallbackFunction(int someParameter)
{
  // DO the real work
}

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

...

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