• 【C++】传递‘类非静态成员函数’用作回调函数


    在C语言中,传递函数指针是非常常见的操作。

    在C++语言中,使用C语言一致的方法传递全局函数指针,或者传递静态函数指针也很常见。

    不过如果遇到想传递非静态成员函数时,可以参考以下示例代码。

    1. #ifndef _WORKER_HPP_
    2. #define _WORKER_HPP_
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. class Worker {
    10. public:
    11. // 设置回调函数
    12. void registerCallback(std::function<void(int, std::string, long)> cb) {
    13. this->mCallback = cb;
    14. }
    15. void startWork() {
    16. using namespace std::literals;
    17. const std::chrono::time_point now = std::chrono::system_clock::now();
    18. const std::time_t t_c = std::chrono::system_clock::to_time_t(now);
    19. int i = 1008;
    20. std::stringstream ss;
    21. ss << std::put_time(std::localtime(&t_c), "%F %T");
    22. std::string s = ss.str();
    23. long l = __cplusplus;
    24. mCallback(i, s, l);
    25. }
    26. private:
    27. std::function<void(int, std::string, long)> mCallback;
    28. };
    29. #endif

    参考Manager内的work函数,列出了几种写法。

    1. #ifndef _MANAGER_HPP_
    2. #define _MANAGER_HPP_
    3. #include
    4. #include
    5. #include
    6. #include "worker.hpp"
    7. class Manager {
    8. public:
    9. Manager(): mI(-1), mS("coco"), mL(-1L) {
    10. }
    11. virtual ~Manager() = default;
    12. public:
    13. void work() {
    14. using namespace std::placeholders;
    15. // 设置回调函数, 使用lambda
    16. worker.registerCallback([this](int&& i, std::string&& s, long&& l) -> void {
    17. this->onMsgCallback(i, s, l);
    18. });
    19. // 设置回调函数,使用bind,搭配mem_fn
    20. auto ptr = std::mem_fn(&Manager::onMsgCallback);
    21. worker.registerCallback(std::bind(ptr, this, _1, _2, _3));
    22. // 不搭配mem_fn
    23. worker.registerCallback(std::bind(&Manager::onMsgCallback, this, _1, _2, _3));
    24. worker.startWork();
    25. }
    26. void print() {
    27. std::cout << __FUNCTION__ << " mI is " << mI << ", mS is " << mS << ", mL is " << mL << std::endl;
    28. }
    29. private:
    30. void onMsgCallback(int i, std::string s, long l) {
    31. std::cout << __FUNCTION__ << " i is " << i << ", s is " << s << ", l is " << l << std::endl;
    32. this->mI = i;
    33. this->mS = s;
    34. this->mL = l;
    35. }
    36. private:
    37. int mI;
    38. std::string mS;
    39. long mL;
    40. Worker worker;
    41. };
    42. #endif

    main示例:

    1. int main()
    2. {
    3. // 演示将非静态成员函数设置为回调函数
    4. {
    5. Manager manager;
    6. manager.print();
    7. manager.work();
    8. manager.print();
    9. }
    10. return 0;
    11. }

     输出参考:

    print mI is -1, mS is coco, mL is -1
    onMsgCallback i is 1009, s is 2023-11-18 20:22:34, l is 201402
    print mI is 1009, mS is 2023-11-18 20:22:34, mL is 201402

  • 相关阅读:
    进入数字化供应链高潮期,与IBM咨询共创无边界竞争力
    python SO3 & so3 BCH近似计算
    【业务功能篇108】CDN Nginx
    tensorflow切片
    有一个不错的解决module xxx has no attribute的思路
    JAVAWeb3:思想总结和问题汇总
    采用 vue3 + vite + element-plus + tsx + decorators + tailwindcss 构建 admin 管理员后台页面
    Java NIO中的Files类的使用
    C语言内功修炼【整型在内存中的存储】
    Qt5开发及实例V2.0-第十五章-Qt单元测试框架
  • 原文地址:https://blog.csdn.net/Taozi825232603/article/details/134485580