免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2829 | 回复: 9
打印 上一主题 下一主题

[C++] 怎么做一个类的回调函数? [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-01-12 19:45 |只看该作者 |倒序浏览
本帖最后由 jd808 于 2015-01-12 19:46 编辑
  1. class a
  2. {
  3. public:
  4.         a();
  5.         ~a();
  6.         int Test1(int num,int a);
  7.         int Test2(int num,int b);
  8.         int Caller(int (*ptr)(int ,int),int n,int t);
  9.         void Call();
  10. };

  11. a::a(){}
  12. a::~a(){}

  13. int a::Test1(int num,int a)
  14. {
  15.   printf("i am test1,the data is %d %d\n",num,a);
  16.   return 0;
  17. }

  18. int a::Test2(int num,int b)
  19. {
  20.   printf("i am test2,the data is %d===%d\n",num,b);
  21.   return 0;
  22. }

  23. int a::Caller(int (*ptr)(int ,int),int n,int t)//指向函数的指针作函数参数,这里第二个参数是函数指针的参数
  24. {
  25.   int a=(*ptr)(n,t);
  26.   return a;
  27. }


  28. void a::Call()
  29. {
  30.         Caller(Test1,20,30);//这里报错
  31.         printf("************************\n");
  32.         Caller(Test2,10,40);//这里报错
  33. }


  34. int main(void)
  35. {

  36.         a *v=new a();
  37.         v->Call();
  38.         return 1
  39. }
复制代码
这样子不行

Multiple markers at this line
        - no matching function for call to ‘a::Caller(<unresolved overloaded function type>, int, int)’
        - candidate is:


这要怎么搞呢?

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
2 [报告]
发表于 2015-01-12 20:24 |只看该作者
  1. #include <stdio.h>
  2. class a
  3. {
  4. public:
  5.         a();
  6.         ~a();
  7.         int Test1(int num,int a);
  8.         int Test2(int num,int b);
  9.         int Caller(int (a::*ptr)(int ,int),int n,int t);
  10.         void Call();
  11. };

  12. a::a(){}
  13. a::~a(){}

  14. int a::Test1(int num,int a)
  15. {
  16.   printf("i am test1,the data is %d %d\n",num,a);
  17.   return 0;
  18. }

  19. int a::Test2(int num,int b)
  20. {
  21.   printf("i am test2,the data is %d===%d\n",num,b);
  22.   return 0;
  23. }

  24. int a::Caller(int (a::*ptr)(int ,int),int n,int t)//指向函数的指针作函数参数,这里第二个参数是函数指针的参数
  25. {
  26.   int a=(this->*ptr)(n,t);
  27.   return a;
  28. }


  29. void a::Call()
  30. {
  31.         Caller(&a::Test1,20,30);//这里报错
  32.         printf("************************\n");
  33.         Caller(&a::Test2,10,40);//这里报错
  34. }


  35. int main(void)
  36. {

  37.         a *v=new a();
  38.         v->Call();
  39.         return 1;
  40. }
复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2015-01-12 20:56 |只看该作者
回复 2# linux_c_py_php


    谢谢,果然可以

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2015-01-12 21:49 |只看该作者
回复 1# jd808


    这样:
  1. #include <functional>
  2. #include <cstdio>

  3. struct a
  4. {
  5.     typedef std::function<int(int,int)> function_type;
  6.     typedef std::function<int(function_type,int,int)> function_caller_type;
  7.     typedef std::function<void()> function_call_type;

  8.     function_type           test1;
  9.     function_type           test2;
  10.     function_caller_type    caller;
  11.     function_call_type      call;

  12.     a(): test1( []( int num, int a ) { printf("i am test1, the data is %d %d\n",num,a); return 0; } ),
  13.          test2( []( int num, int a ) { printf("i am test2, the data is %d == %d\n",num,a); return 0; } ),
  14.          caller( []( function_type func, int n, int t ) { return func( n, t ); } ),
  15.          call( [this](){ (*this).caller( (*this).test1, 20, 30 ); printf("************************\n"); (*this).caller( (*this).test2, 10,40 ); } )
  16.          {}
  17. };

  18. int main()
  19. {
  20.     a v;
  21.     v.call();
  22.     return 0;
  23. }
复制代码

论坛徽章:
44
15-16赛季CBA联赛之浙江
日期:2021-10-11 02:03:59程序设计版块每日发帖之星
日期:2016-07-02 06:20:0015-16赛季CBA联赛之新疆
日期:2016-04-25 10:55:452016科比退役纪念章
日期:2016-04-23 00:51:2315-16赛季CBA联赛之山东
日期:2016-04-17 12:00:2815-16赛季CBA联赛之福建
日期:2016-04-12 15:21:2915-16赛季CBA联赛之辽宁
日期:2016-03-24 21:38:2715-16赛季CBA联赛之福建
日期:2016-03-18 12:13:4015-16赛季CBA联赛之佛山
日期:2016-02-05 00:55:2015-16赛季CBA联赛之佛山
日期:2016-02-04 21:11:3615-16赛季CBA联赛之天津
日期:2016-11-02 00:33:1215-16赛季CBA联赛之浙江
日期:2017-01-13 01:31:49
5 [报告]
发表于 2015-01-12 22:27 |只看该作者
用std::function和bind吧,都什么年代了……

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
6 [报告]
发表于 2015-01-13 09:36 |只看该作者
windoze 发表于 2015-01-12 22:27
用std::function和bind吧,都什么年代了……

给个案例看看吧

论坛徽章:
13
双鱼座
日期:2013-10-23 09:30:05数据库技术版块每日发帖之星
日期:2016-04-20 06:20:00程序设计版块每日发帖之星
日期:2016-03-09 06:20:002015亚冠之塔什干火车头
日期:2015-11-02 10:07:452015亚冠之德黑兰石油
日期:2015-08-30 10:07:07数据库技术版块每日发帖之星
日期:2015-08-28 06:20:00数据库技术版块每日发帖之星
日期:2015-08-05 06:20:002015年迎新春徽章
日期:2015-03-04 09:57:09辰龙
日期:2014-12-03 14:45:52酉鸡
日期:2014-07-23 09:46:23亥猪
日期:2014-03-13 08:46:22金牛座
日期:2014-02-11 09:36:21
7 [报告]
发表于 2015-01-13 09:59 |只看该作者
二炮牛逼

论坛徽章:
44
15-16赛季CBA联赛之浙江
日期:2021-10-11 02:03:59程序设计版块每日发帖之星
日期:2016-07-02 06:20:0015-16赛季CBA联赛之新疆
日期:2016-04-25 10:55:452016科比退役纪念章
日期:2016-04-23 00:51:2315-16赛季CBA联赛之山东
日期:2016-04-17 12:00:2815-16赛季CBA联赛之福建
日期:2016-04-12 15:21:2915-16赛季CBA联赛之辽宁
日期:2016-03-24 21:38:2715-16赛季CBA联赛之福建
日期:2016-03-18 12:13:4015-16赛季CBA联赛之佛山
日期:2016-02-05 00:55:2015-16赛季CBA联赛之佛山
日期:2016-02-04 21:11:3615-16赛季CBA联赛之天津
日期:2016-11-02 00:33:1215-16赛季CBA联赛之浙江
日期:2017-01-13 01:31:49
8 [报告]
发表于 2015-01-13 10:17 |只看该作者
回复 6# jd808
  1. #include <functional>
  2. using namespace std::placeholders;
  3. class a
  4. {
  5. public:
  6.         a();
  7.         ~a();
  8.         int Test1(int num,int a);
  9.         int Test2(int num,int b);
  10.         int Caller(std::function<int(int, int)> f,int n,int t);
  11.         void Call();
  12. };

  13. a::a(){}
  14. a::~a(){}

  15. int a::Test1(int num,int a)
  16. {
  17.   printf("i am test1,the data is %d %d\n",num,a);
  18.   return 0;
  19. }

  20. int a::Test2(int num,int b)
  21. {
  22.   printf("i am test2,the data is %d===%d\n",num,b);
  23.   return 0;
  24. }

  25. int a::Caller(std::function<int(int, int)> f,int n,int t)//指向函数的指针作函数参数,这里第二个参数是函数指针的参数
  26. {
  27.   int a=f(n,t);
  28.   return a;
  29. }


  30. void a::Call()
  31. {
  32.         Caller(std::bind(&a::Test1, this, _1, _2),20,30);//这里报错
  33.         printf("************************\n");
  34.         Caller(std::bind(&a::Test2, this, _1, _2),10,40);//这里报错
  35. }


  36. int main(void)
  37. {

  38.         a *v=new a();
  39.         v->Call();
  40.         return 1;
  41. }
复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
9 [报告]
发表于 2015-01-13 16:09 |只看该作者
回复 8# windoze


    谢谢,可以了,不过我的是在std::tr1::function下面,不在std库了,搞的我折腾了半天

论坛徽章:
44
15-16赛季CBA联赛之浙江
日期:2021-10-11 02:03:59程序设计版块每日发帖之星
日期:2016-07-02 06:20:0015-16赛季CBA联赛之新疆
日期:2016-04-25 10:55:452016科比退役纪念章
日期:2016-04-23 00:51:2315-16赛季CBA联赛之山东
日期:2016-04-17 12:00:2815-16赛季CBA联赛之福建
日期:2016-04-12 15:21:2915-16赛季CBA联赛之辽宁
日期:2016-03-24 21:38:2715-16赛季CBA联赛之福建
日期:2016-03-18 12:13:4015-16赛季CBA联赛之佛山
日期:2016-02-05 00:55:2015-16赛季CBA联赛之佛山
日期:2016-02-04 21:11:3615-16赛季CBA联赛之天津
日期:2016-11-02 00:33:1215-16赛季CBA联赛之浙江
日期:2017-01-13 01:31:49
10 [报告]
发表于 2015-01-13 16:46 |只看该作者
回复 9# jd808

编译器太老了,升级GCC4.9/Clang 3.5/VC2013(好吧,这是目前发布正式版的最新版本了)……
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP