代码示例
- /*
- demo:
- std::function<>和std::bind()配合 实现回调函数
- */
-
- #include
- #include
- using namespace std;
- #include
- #include
-
- typedef std::function<void(const string&)> ReplyCb;
-
- //网络请求类(请求登陆)
- class ReqLogin
- {
- public:
- ReqLogin();
- ~ReqLogin();
-
- void request(ReplyCb successHandler, ReplyCb errorHandler, bool heap = false)
- {
- Sleep(1);
- //... do somethings
-
- //回调发送方:告诉他人登陆成功
- successHandler("suc");
- }
-
- private:
-
- };
-
- ReqLogin::ReqLogin()
- {
- }
-
- ReqLogin::~ReqLogin()
- {
- }
-
- //用户管理类
- class UserManager
- {
- public:
- UserManager();
- ~UserManager();
-
- void login_func() {
- ReqLogin login;
- login.request(std::bind(&UserManager::onLoginReply, this, std::placeholders::_1),
- NULL);
-
- }
-
- //回调接收方
- void onLoginReply(const string& msg) {
- cout << "onLoginReply()..." << msg << endl;
- }
- private:
-
- };
-
- UserManager::UserManager()
- {
- }
-
- UserManager::~UserManager()
- {
- }
-
-
- int main()
- {
- UserManager UM;
- UM.login_func();
-
- return 0;
- }
【精选】【C++】C++11的std::function和std::bind用法【精选】【C++】C++11的std::function和std::bind用法详解_c++中的std中的方法-CSDN博客
- #include
- #include
-
- class A {
- public:
- void fun_3(int k, int m) {
- std::cout << "print: k = " << k << ", m = " << m << std::endl;
- }
- };
-
- void fun_1(int x, int y, int z) {
- std::cout << "print: x = " << x << ", y = " << y << ", z = " << z << std::endl;
- }
-
- void fun_111(int x, int y, int z,int m, int n) {
- std::cout << "print: x = " << x << ", y = " << y << ", z = " << z
- << ", m = " << m << ", n = " << n << std::endl;
- }
-
- void fun_2(int& a, int& b) {
- ++a;
- ++b;
- std::cout << "print: a = " << a << ", b = " << b << std::endl;
- }
-
- int main(int argc, char* argv[]) {
-
- //f1的类型为 function
- auto f1 = std::bind(fun_1, 1, 2, 3); //表示绑定函数 fun 的第一,二,三个参数值为: 1 2 3
- f1(); //print: x=1,y=2,z=3
-
- auto f2 = std::bind(fun_1, std::placeholders::_1, std::placeholders::_2, 3);
- //表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f2 的第一,二个参数指定
- f2(1, 2); //print: x=1,y=2,z=3
-
- //wgj总结:std::placeholders::_1,其中_1指的是fun_111()中的第一个参数,
- //而f111(3,2,1)调用时,f111中的第一个参数对应fun_111()中的第三个参数
- auto f111 = std::bind(fun_111, std::placeholders::_3, std::placeholders::_2, std::placeholders::_1, 4,5);
- f111(3,2,1);
-
- auto f3 = std::bind(fun_1, std::placeholders::_2, std::placeholders::_1, 3);
- //表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f3 的第二,一个参数指定
- //注意: f2 和 f3 的区别。
- f3(1, 2); //print: x=2,y=1,z=3
-
- int m = 2;
- int n = 9;
- auto f4 = std::bind(fun_2, std::placeholders::_1, n); //表示绑定fun_2的第一个参数为n, fun_2的第二个参数由调用f4的第一个参数(_1)指定。
- //wgj 此处应该是写反了!!!应该为:绑定fun_2的第二个参数n,fun_2 的第一个参数由调用f4的第一个参数指定
- f4(m); //print: a=3,b=4
- std::cout << "m = " << m << std::endl; //m=3 说明:bind对于不事先绑定的参数,通过std::placeholders传递的参数是通过引用传递的,如m
- std::cout << "n = " << n << std::endl; //n=3 说明:bind对于预先绑定的函数参数是通过值传递的,如n
-
- A a;
- //f5的类型为 function
- auto f5 = std::bind(&A::fun_3, a, std::placeholders::_1, std::placeholders::_2); //使用auto关键字
- f5(10, 20); //调用a.fun_3(10,20),print: k=10,m=20
-
- std::function<void(int, int)> fc = std::bind(&A::fun_3, a, std::placeholders::_1, std::placeholders::_2);
- fc(10, 20); //调用a.fun_3(10,20) print: k=10,m=20
-
- return 0;
- }