std :: bind ()
std :: bind is mainly used for binding to generate an objective function, typically used to generate a callback function, cocos rollback functions are by std :: bind and std :: function to achieve. To understand two points:
1. The binding function of a global or static variable is less than the binding members of the member function, and does not require the following references
// binding global function
Auto STD :: = pFunc the bind (func1,. 3);
/ / bind static function
pFunc = std :: the bind (Func :: func6, 3);
// bind before class member function, you need to add a reference function, a multi-variable parameter Fuc
Func FUNC;
pFunc = std :: the bind (& Func func2 ::, FUNC,. 3);
2. placeholder std :: placeholders :: _ 1 to herein represent input parameters, and the parameters in the first row, code analysis more apparent
#include <the iostream>
#include < Functional>
the using namespace STD;
class Func
{
public:
static void Func6 (NUMA int, int Numb, int NumC)
{
std::cout << numa << " " << numb << " " << numc << endl;
}
void func2(int numa, int numb, int numc,std::string name)
{
std::cout << numa << " " << numb << " " << numc<<" " << name<< endl ;
}
}
void callFunc(std::function<void(int a,int b)> call)
{
call(1,2);
}
void func1(int numa, int numb, int numc)
{
std::cout << numa << " " << numb << " " << numc << endl;
}
int main()
{
callFunc(std::bind(func1, std::placeholders::_1, std::placeholders::_2, 3));
callFunc(std::bind(func1, std::placeholders::_2, std::placeholders::_1, 3));
callFunc(std::bind(func1, std::placeholders::_2, 3, std::placeholders::_1));
callFunc(std::bind(Func::func6, std::placeholders::_1, std::placeholders::_2, 3));
callFunc(std::bind(&Func::func2, func, std::placeholders::_1, std::placeholders::_2, 3, "name"));
}
运行结果如下
:: function std
std :: function equal to a function pointer, function pointer easier to use compared to, record it several uses: to the global or static function, class member functions, Lambda expressions and simulation functions. Point to global functions or use std :: function <void ()> static function testFunc = func3, point to a class member function, member variables need to develop the class the function belongs testFunc = std :: bind (& Func :: func2, func, 1, 2, 3, "name "). Code is as follows:
#include <the iostream>
#include <String>
#include <the iostream>
#include <Functional>
the using namespace STD;
class Func
{
public:
int SUM;
Func ()
{
SUM = 2;
}
void func2 (int NUMA, Numb int, int NumC, STD :: String name)
{
STD :: COUT NUMA << << "" << << Numb "
}
void func4()
{
std::cout << "func4" << endl;
}
void operator() (int a,int b)
{
std::cout << "Operator:" << sum<<" "<<a<<" "<<b<<endl;
}
static void func6(int numa, int numb, int numc)
{
std::cout << numa << " " << numb << " " << numc << endl;
}
static void func5()
{
std::cout << "static func" << endl;
}
};
void callFunc(std::function<void(int a,int b)> call)
{
call(1,2);
}
void func3()
{
STD :: COUT << "func3" << endl;
}
void func1 (int NUMA, Numb int, int NumC)
{
STD :: COUT NUMA << << "" << Numb << "" << NumC < <endl;
}
int main ()
{
Func FUNC;
int SUM = 10;
int resultInt = 0;
// global or static function
std :: cout << "global or static function" << endl;
STD :: function <void ()> = func3 testFunc;
testFunc ();
testFunc :: = Func Func5;
testFunc ();
// member function
std :: cout << "member function" << endl;
testFunc = std::bind(&Func::func2, func, 1, 2, 3, "name");
testFunc();
//Lambda表达式
std::cout << "Lambda表达式" << endl;
testFunc = [=, &resultInt](){std::cout << sum << endl; resultInt += 100; };
testFunc();
cout << "the reslutInt is " << resultInt << endl;
//仿函数
std::cout << "仿函数" << endl;
std::function<void(int a, int b)> abFunc = func;
abFunc(10, 20);
std::cout << resultInt << std::endl;
}