Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9147405
  • 博文数量: 1726
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19850
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1726)

文章存档

2024年(2)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: 其他平台

2021-02-02 19:50:53

解决的问题:
  经常我们做业务共用代码 HPP 时, 希望CPPx1包含的时候有自己的实现函数, CPPx2是另外一个.
那么怎么在HPP动态调用不同的实现呢? 一下是做了个例子.  其实还可以使用 模板类检测类内是否有某个函数或者成员变量.  https://blog.csdn.net/sinat_18811413/article/details/104606673

点击(此处)折叠或打开

  1. #include <thread>
  2. #include <functional>
  3. #include <iostream>
  4. #include <ostream>
  5. #include <unistd.h>
  6. using namespace std;

  7. /*
  8.     g++ cpp_func_ptr.cpp -lpthread
  9. */

  10. class CTest;
  11. typedef void (CTest::*CUST_CB)();
  12. class CTest
  13. {
  14. public:
  15.     std::thread m_thread ;

  16.     CTest()
  17.     {
  18.         //cb = &CTest::custom_cb;
  19.         m_thread = std::thread(std::bind(&CTest::execute, this));
  20.     }

  21.     void custom_cb()
  22.     {
  23.         cout << "callback OK" << endl;
  24.     }

  25.     void regist_cb(CUST_CB pfunc_cb)
  26.     {
  27.         cb = pfunc_cb;
  28.     }

  29.     void execute(void)
  30.     {
  31.         while (1) {
  32.             if (cb) {
  33.                 (this->*cb)();
  34.             }
  35.             sleep(1);
  36.         }
  37.     }
  38.     
  39.     CUST_CB cb{nullptr};
  40. };

  41. int main()
  42. {
  43.     CTest test;
  44.     test.regist_cb(&CTest::custom_cb);
  45.     test.execute();
  46. }

阅读(2554) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~