1.闭包
含义:带有上下文的函数(有状态的函数)。函数时代码,状态就是一组变量,将代码与一组变量捆绑,就形成了闭包(比较像一个类)。
程序的闭包捆绑,必须发生在运行时

2.可调用对象
函数指针、具有operator成员函数的类对象(仿函数)、可被转换成函数指针的类对象、类成员函数指针或者类成员指针都可以被称为可调用对象
- //函数指针
- void func(int num,string name,int num2) {
- cout << "num = " << num << ",name = " << name << endl;
- }
- using funcptr = void(*)(int,string,int);
-
- class CC
- {
- public:
- //具有operator成员函数的类对象(仿函数)重载()
- void operator()(string str) {
- cout << "仿函数 str = " << str << endl;
- }
-
- //可被转换成函数指针的类对象
- operator funcptr() {
- //return hh; //error
- return ww;
- }
- //属于对象
- void hh(int val, string str,int num2) {
- cout << "val = " << val << " str = " << str << endl;
- }
- //属于类
- static void ww(int val, string str,int num2) {
- cout << "val = " << val << " str = " << str << endl;
- }
-
- int id =10;
- }
-
-
- //调用
- //函数指针
- cout << "函数指针 : ";
- funcptr2(22, "lili",22);
-
- //仿函数
- CC c;
- c("hgello world");
-
- //类对象转换的函数指针
- CC cc;
- cout << "类对象转换的函数指针 : ";
- cc(67,"xiaoli",67);
-
-
- CC cccc;
- //类的成员函数指针
- funcptr func = CC::ww;
- using fptr = void(CC::*)(int,string,int);
- fptr fp = &CC::hh;
- cout << "类成员函数指针 fp : ";
- (cccc.*fp)(333, "ddd",333);
-
- //类的成员指针(变量)
- using ptr1 = int CC::*;
- ptr1 pt = &CC::id;
- cccc.*pt = 1000;
- cout << "类成员指针: id = " << cccc.id << endl;
3. 可调用对象包装器(std::function)模板类
std::function<返回值类型(参数列表)> diy_name = 可调用对象
- /********与可待用对象连一起看**********/
- //包装普通函数
- function<void(int, string,int)> f1 = funcTT;
- cout << "包装普通函数 : ";
- f1(111, "aaaaaaaaaa",111);
-
- //包装类的静态函数
- function<void(int, string,int)> f2 = CC::ww;
- cout << "包装类的静态函数 : ";
- f2(222,"bbbbbbbbbb",222);
-
- //包装仿函数
- CC c;
- function<void( string)> f3 = c;
- cout << "包装仿函数 : ";
- f3("ccccccccccc");
-
- //包装可被转换成函数指针的类对象
- CC cc;
- function<void(int, string,int)> f4 = cc;
- cout << "包装可被转换成函数指针的类对象 : ";
- f4(444, "dddddddddd",444);
4.可调用对象绑定器(std::bind)
std::bind用来将对象与参数绑定,绑定后的结果可以使用std::function进行保存,并延迟调用到我们任何需要的时候。
其作用:
1.将可调用对象与其参数一起绑定成一个仿函数
2.将多元(参数个数为n,n>1)可调用对象转换成一元或者(n-1)元可调用对象,即只
绑定部分参数
- //绑定非类成员函数\变量
- auto f = bind(可调用地址\函数名, 绑定的参数或者占位符);
- // 绑定类成员函数
- auto f = bind(类函数,类实例对象地址, 绑定的参数或者占位符);
- // 绑定类成员变量
- auto f = bind(类成员变量, 类实例对象地址);
绑定非类成员函数
- void outputAdd(int x, int y) {
- cout << "x = " << x << " , y = " << y << " ,x+ y = " <<x+ y << endl;
- }
- void outputTest(int a,int b,const function<void(int,int)> &f) {
- if (a % 2 == 0) {
- f(a,b);
- }
- }
-
- for (int i = 0; i < 10;++i) {
- auto f = bind(outputAdd, i + 100, i + 200);
- outputTest(i,i,f);
-
- auto f1 = bind(outputAdd, placeholders::_1, placeholders::_2);
- outputTest(i, i, f1);
- }
绑定类成员函数与变量
- class DD
- {
- public:
- void out(int x, int y) {
- cout << "x = " << x << " , y = " << y << endl;
- }
- int m_num = 99;
- };
-
- DD d;
- bind(&DD::out, &d, 520, 1314);
- bind(&DD::out,&d,520, placeholders::_1)(1314);
- auto ff = bind(&DD::out, &d, 520, placeholders::_1);//ff为仿函数
- ff(666);//二元降为了一元
- function<void(int)> fff= ff;//将ff包装成一个可调用对象fff
- function<void(int ,int)> ffff = ff;//将ff包装成一个可调用对象ffff
- cout << "将类成员函数包装成一个可调用对象:" << endl;
- fff(123);
- ffff(123,456);//只传入了123
-
-
- cout << "绑定类成员变量:" << endl;
- //绑定类成员函数
- auto rr = bind(&DD::m_num, &d);//rr为仿函数
- cout << "rr = " << rr()<<endl;
- rr() = 123;
- cout << "rr = " << rr() << endl;
- cout << "将类成员变量包装成一个可调用对象:" << endl;
- function<int& (void)> rrr = rr;//将rr包装成一个可调用对象
- cout << "rrr = " << rrr() << endl;
- rrr() = 22;
- cout << "rrr = " << rrr() << endl;