• C++11的std::function和std::bind用法


    代码示例

    1. /*
    2. demo:
    3. std::function<>和std::bind()配合 实现回调函数
    4. */
    5. #include
    6. #include
    7. using namespace std;
    8. #include
    9. #include
    10. typedef std::function<void(const string&)> ReplyCb;
    11. //网络请求类(请求登陆)
    12. class ReqLogin
    13. {
    14. public:
    15. ReqLogin();
    16. ~ReqLogin();
    17. void request(ReplyCb successHandler, ReplyCb errorHandler, bool heap = false)
    18. {
    19. Sleep(1);
    20. //... do somethings
    21. //回调发送方:告诉他人登陆成功
    22. successHandler("suc");
    23. }
    24. private:
    25. };
    26. ReqLogin::ReqLogin()
    27. {
    28. }
    29. ReqLogin::~ReqLogin()
    30. {
    31. }
    32. //用户管理类
    33. class UserManager
    34. {
    35. public:
    36. UserManager();
    37. ~UserManager();
    38. void login_func() {
    39. ReqLogin login;
    40. login.request(std::bind(&UserManager::onLoginReply, this, std::placeholders::_1),
    41. NULL);
    42. }
    43. //回调接收方
    44. void onLoginReply(const string& msg) {
    45. cout << "onLoginReply()..." << msg << endl;
    46. }
    47. private:
    48. };
    49. UserManager::UserManager()
    50. {
    51. }
    52. UserManager::~UserManager()
    53. {
    54. }
    55. int main()
    56. {
    57. UserManager UM;
    58. UM.login_func();
    59. return 0;
    60. }

       【精选】【C++】C++11的std::function和std::bind用法【精选】【C++】C++11的std::function和std::bind用法详解_c++中的std中的方法-CSDN博客

    补充std::bind()的用法

    1. #include
    2. #include
    3. class A {
    4. public:
    5. void fun_3(int k, int m) {
    6. std::cout << "print: k = " << k << ", m = " << m << std::endl;
    7. }
    8. };
    9. void fun_1(int x, int y, int z) {
    10. std::cout << "print: x = " << x << ", y = " << y << ", z = " << z << std::endl;
    11. }
    12. void fun_111(int x, int y, int z,int m, int n) {
    13. std::cout << "print: x = " << x << ", y = " << y << ", z = " << z
    14. << ", m = " << m << ", n = " << n << std::endl;
    15. }
    16. void fun_2(int& a, int& b) {
    17. ++a;
    18. ++b;
    19. std::cout << "print: a = " << a << ", b = " << b << std::endl;
    20. }
    21. int main(int argc, char* argv[]) {
    22. //f1的类型为 function
    23. auto f1 = std::bind(fun_1, 1, 2, 3); //表示绑定函数 fun 的第一,二,三个参数值为: 1 2 3
    24. f1(); //print: x=1,y=2,z=3
    25. auto f2 = std::bind(fun_1, std::placeholders::_1, std::placeholders::_2, 3);
    26. //表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f2 的第一,二个参数指定
    27. f2(1, 2); //print: x=1,y=2,z=3
    28. //wgj总结:std::placeholders::_1,其中_1指的是fun_111()中的第一个参数,
    29. //而f111(3,2,1)调用时,f111中的第一个参数对应fun_111()中的第三个参数
    30. auto f111 = std::bind(fun_111, std::placeholders::_3, std::placeholders::_2, std::placeholders::_1, 4,5);
    31. f111(3,2,1);
    32. auto f3 = std::bind(fun_1, std::placeholders::_2, std::placeholders::_1, 3);
    33. //表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f3 的第二,一个参数指定
    34. //注意: f2 和 f3 的区别。
    35. f3(1, 2); //print: x=2,y=1,z=3
    36. int m = 2;
    37. int n = 9;
    38. auto f4 = std::bind(fun_2, std::placeholders::_1, n); //表示绑定fun_2的第一个参数为n, fun_2的第二个参数由调用f4的第一个参数(_1)指定。
    39. //wgj 此处应该是写反了!!!应该为:绑定fun_2的第二个参数n,fun_2 的第一个参数由调用f4的第一个参数指定
    40. f4(m); //print: a=3,b=4
    41. std::cout << "m = " << m << std::endl; //m=3 说明:bind对于不事先绑定的参数,通过std::placeholders传递的参数是通过引用传递的,如m
    42. std::cout << "n = " << n << std::endl; //n=3 说明:bind对于预先绑定的函数参数是通过值传递的,如n
    43. A a;
    44. //f5的类型为 function
    45. auto f5 = std::bind(&A::fun_3, a, std::placeholders::_1, std::placeholders::_2); //使用auto关键字
    46. f5(10, 20); //调用a.fun_3(10,20),print: k=10,m=20
    47. std::function<void(int, int)> fc = std::bind(&A::fun_3, a, std::placeholders::_1, std::placeholders::_2);
    48. fc(10, 20); //调用a.fun_3(10,20) print: k=10,m=20
    49. return 0;
    50. }

  • 相关阅读:
    Web攻防05_MySQL_二次注入&堆叠注入&带外注入
    2023CSP-S初赛复习整理
    Spring Boot面试题
    电脑回收站为什么自动清空?win10回收站自动清理的东西怎么找回
    【2023届秋招】网易算法岗
    【开源】基于Vue.js的大学兼职教师管理系统的设计和实现
    NFC隐藏功能大公开:乘车刷门禁,NFC实用无风险
    Lua5.4源码剖析:二. 详解String数据结构及操作算法
    有序单链表的插入删除操作
    tableau高级绘图(二)-在tableau中创建雷达图(不修改数据源)
  • 原文地址:https://blog.csdn.net/QQ1402369668/article/details/134026441