Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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++ 将一个对象的函数传递给另一个类的另一个std::函数_C++_C++11_Std Function_Stdbind - Fatal编程技术网

C++ 将一个对象的函数传递给另一个类的另一个std::函数

C++ 将一个对象的函数传递给另一个类的另一个std::函数,c++,c++11,std-function,stdbind,C++,C++11,Std Function,Stdbind,将一个类的成员函数传递给另一个类的std::函数的正确方法是什么 例如,下面的栏想要存储一个Foo对象的函数 这不会编译: no match for 'operator=' (operand types are 'std::function<bool(int)>' and 'std::_Bind_helper<false, bool (Foo::*)(int), Foo*>::type {aka std::_Bind<std::_Mem_fn<bool (Fo

将一个类的成员函数传递给另一个类的std::函数的正确方法是什么

例如,下面的栏想要存储一个Foo对象的函数

这不会编译:

no match for 'operator=' (operand types are 'std::function<bool(int)>' and 'std::_Bind_helper<false, bool (Foo::*)(int), Foo*>::type {aka std::_Bind<std::_Mem_fn<bool (Foo::*)(int)>(Foo*)>}')**
您可以使用lambda:

bar.testFunction = [&foo1](int x){ return foo1.isEven(x); };
您可以使用lambda:

bar.testFunction = [&foo1](int x){ return foo1.isEven(x); };
isEven接受稍后将要传递的参数,因此需要添加占位符来指示未绑定的参数

bar.testFunction = std::bind(&Foo::isEven, &foo1, std::placeholders::_1);
或者用lambda代替bind

isEven接受稍后将要传递的参数,因此需要添加占位符来指示未绑定的参数

bar.testFunction = std::bind(&Foo::isEven, &foo1, std::placeholders::_1);
或者用lambda代替bind