文章目录
- 1.可调用对象
- 1.1 函数指针
- 1.2 具有operator()成员函数的类对象/仿函数/函数对象
- 1.3 可被转换为函数指针的类对象
- 1.4 类成员函数指针
- 2.std::function
- 2.1 绑定普通函数
- 2.2 绑定类的静态成员函数
- 2.3 绑定具有operator()成员函数的类对象
- 2.4 绑定可被转换为函数指针的类对象
- 3.std::bind()
- 3.1 绑定普通函数
- 3.2 绑定类的成员函数
- 3.3 绑定具有operator()成员函数的类对象
- 3.4 bind() 和 function 配合使用
1.可调用对象
1.1 函数指针
#include <iostream>
using namespace std;
void fun(int v)
{
cout << "fun()函数执行了,v = " << v << endl;
}
int main()
{
void(*pfun)(int) = fun;
pfun(15);
return 0;
}
1.2 具有operator()成员函数的类对象/仿函数/函数对象
#include <iostream>
using namespace std;
class TC
{
public:
void operator()(int tv) // 函数调用运算符
{
cout << "TC::operator()执行了,tv = " << tv << endl;
}
};
int main()
{
TC tc;
tc(20); // 等价于tc.operator()(20);
return 0;
}
1.3 可被转换为函数指针的类对象
#include <iostream>
using namespace std;
class TC
{
public:
using pfun = void(*)(int); // 使用using指定别名
static void fun(int tv) // 静态成员函数
{
cout << "TC::fun()静态成员函数执行了,tv = " << tv << endl;
}
operator pfun() // 类型转换运算符,能把本类类型对象转换成一个函数指针
{
return fun;
}
};
int main()
{
TC tc;
tc(50); // 等价于tc.operator TC::pfun()(50);
return 0;
}
1.4 类成员函数指针
#include <iostream>
using namespace std;
class TC
{
public:
void fun(int tv)
{
cout << "TC::fun()执行了,tv = " << tv << endl;
}
};
int main()
{
TC tc;
void(TC::*pfun)(int) = &TC::fun; // 类成员函数指针变量pfun的定义与初始化
(tc.*pfun)(50); // 通过对象tc调用成员函数fun
return 0;
}
2.std::function
头文件为 #include <functional>
std::function,即可调用对象包装器,它是个类模板,通过给它指定模板参数,它就能够用统一的方式来处理各种可调用对象(类成员函数指针除外)。
2.1 绑定普通函数
#include <iostream>
#include <functional>
using namespace std;
void fun(int v)
{
cout << "fun()函数执行了,v = " << v << endl;
}
int main()
{
std::function<void(int)> f1 = fun;
f1(100);
return 0;
}
2.2 绑定类的静态成员函数
#include <iostream>
#include <functional>
using namespace std;
class TC
{
public:
static int fun(int tv) // 静态成员函数
{
cout << "TC::fun()静态成员函数执行了,tv = " << tv << endl;
return tv;
}
};
int main()
{
std::function<int(int)> f2 = TC::fun;
cout << f2(100) << endl;
return 0;
}
2.3 绑定具有operator()成员函数的类对象
#include <iostream>
#include <functional>
using namespace std;
class TC
{
public:
void operator()(int tv) // 函数调用运算符
{
cout << "TC::operator()执行了,tv = " << tv << endl;
}
};
int main()
{
TC tc;
std::function<void(int)> f3 = tc;
f3(100);
return 0;
}
2.4 绑定可被转换为函数指针的类对象
#include <iostream>
#include <functional>
using namespace std;
class TC
{
public:
using pfun = void(*)(int); // 使用using指定别名
static void fun(int tv) // 静态成员函数
{
cout << "TC::fun()静态成员函数执行了,tv = " << tv << endl;
}
operator pfun() // 类型转换运算符,能把本类类型对象转换成一个函数指针
{
return fun;
}
};
int main()
{
TC tc;
std::function<void(int)> f4 = tc;
f4(100);
return 0;
}
3.std::bind()
头文件:#include <functional>
格式:std::bind(待绑定的函数指针/成员函数指针/函数对象, 绑定参数值1, ..., 绑定参数值n);
std::bind() 返回的是仿函数类型对象。
3.1 绑定普通函数
将可调用对象和参数绑定到一起,构成一个仿函数,所以可以直接调用。
#include <iostream>
#include <functional>
using namespace std;
void fun(int x, int y, int z)
{
cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
}
int main()
{
auto bf = std::bind(fun, 10, 20, 30);
bf();
return 0;
}
如果函数有多个参数,可以绑定部分参数,其他的参数在调用的时候指定。
#include <iostream>
#include <functional>
using namespace std;
void fun(int x, int y, int z)
{
cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
}
int main()
{
auto bf = std::bind(fun, placeholders::_1, 20, placeholders::_2);
bf(10, 30);
return 0;
}
#include <iostream>
#include <functional>
using namespace std;
void fun(int x, int y, int z)
{
cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
}
int main()
{
std::bind(fun, placeholders::_1, 20, placeholders::_1)(10, 30);
return 0;
}
std::bind() 对于预先绑定的函数参数是通过值传递的,对于通过 placeholders 占位的参数是通过引用传递的。
#include <iostream>
#include <functional>
using namespace std;
void fun(int& x, int& y)
{
x++;
y++;
}
int main()
{
int a = 2;
int b = 3;
auto bf = std::bind(fun, a, placeholders::_1);
bf(b);
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
3.2 绑定类的成员函数
在下面代码中,CQ 类本身不是仿函数,第二个参数 cq
会导致调用 CQ 类的拷贝构造函数生成一个 CQ 类型的临时对象作为 std::bind() 的返回值。
#include <iostream>
#include <functional>
using namespace std;
class CQ
{
public:
CQ()
{
cout << "构造函数 - " << this << endl;
}
CQ(const CQ&)
{
cout << "拷贝构造函数 - " << this << endl;
}
~CQ()
{
cout << "析构函数 - " << this << endl;
}
void fun(int x, int y)
{
cout << "x = " << x << ", y = " << y << endl;
}
};
int main()
{
CQ cq;
auto bf = std::bind(&CQ::fun, cq, std::placeholders::_1, std::placeholders::_2);
bf(10, 30);
return 0;
}
在下面代码中,CQ 类本身不是仿函数,第二个参数是 &cq
,这样就不生成 CQ 类型的临时对象了,此时 std::bind() 返回的就是 cq
对象本身。
#include <iostream>
#include <functional>
using namespace std;
class CQ
{
public:
CQ()
{
cout << "构造函数 - " << this << endl;
}
CQ(const CQ&)
{
cout << "拷贝构造函数 - " << this << endl;
}
~CQ()
{
cout << "析构函数 - " << this << endl;
}
void fun(int x, int y)
{
cout << "x = " << x << ", y = " << y << endl;
}
};
int main()
{
CQ cq;
auto bf = std::bind(&CQ::fun, &cq, std::placeholders::_1, std::placeholders::_2);
bf(10, 30);
return 0;
}
3.3 绑定具有operator()成员函数的类对象
#include <iostream>
#include <functional>
using namespace std;
class CQ
{
public:
CQ()
{
cout << "构造函数 - " << this << endl;
}
CQ(const CQ&)
{
cout << "拷贝构造函数 - " << this << endl;
}
~CQ()
{
cout << "析构函数 - " << this << endl;
}
void operator()()
{
cout << "operator() - " << this << endl;
}
};
int main()
{
auto bf = std::bind(CQ());
bf();
return 0;
}
3.4 bind() 和 function 配合使用
#include <iostream>
#include <functional>
using namespace std;
class CQ
{
public:
CQ()
{
cout << "构造函数 - " << this << endl;
}
CQ(const CQ&)
{
cout << "拷贝构造函数 - " << this << endl;
}
~CQ()
{
cout << "析构函数 - " << this << endl;
}
void fun(int x, int y)
{
cout << "x = " << x << ", y = " << y << endl;
}
};
int main()
{
CQ cq;
std::function<void(int, int)> bf = std::bind(&CQ::fun, cq, std::placeholders::_1, std::placeholders::_2);
bf(10, 30);
return 0;
}