C++ 类成员函数作为参数

 1 #include <iostream>
 2 using namespace std;
 3 
 4 typedef int int32_t;
 5 
 6 struct IMsgBody{
 7     int body;
 8 };
 9 
10 struct Arg{
11     int arg;
12 };
13 
14 class A;
15 
16 typedef int32_t (A::*GetArg_Fun)(IMsgBody *pMsgBody, Arg *stArg);    //函数指针
17 
18 class A
19 {
20 public:
21     A(){}
22     ~A(){}
23     int32_t GetArg_GameEnd(IMsgBody *pMsgBody,Arg *stArg);
24     int32_t GetTaskArg(IMsgBody *pMsgBody,Arg *stArg,GetArg_Fun getArg_Fun);
25     int32_t GetTaskArg(IMsgBody *pMsgBody,Arg *stArg);
26 };
27 
28 int32_t main()
29 {
30     IMsgBody msgBody;
31     Arg arg;
32     msgBody.body = 78;
33     arg.arg = 45;
34     A a;
35     a.GetTaskArg(&msgBody,&arg);
36     return 0;
37 }
38 
39 int32_t A::GetArg_GameEnd(IMsgBody *pMsgBody,Arg *stArg)
40 {
41     cout<<"pMsgBody:"<<pMsgBody->body<<endl;
42     cout<<"Arg:"<<stArg->arg<<endl;
43     return 123;
44 }
45 
46 int32_t A::GetTaskArg(IMsgBody *pMsgBody,Arg *stArg,GetArg_Fun getArg_Fun)
47 {
48     int ret = 0;
49     ret = (this->*getArg_Fun)(pMsgBody,stArg);
50     cout<<"ret = "<<ret<<endl;
51     return 0;
52 }
53 
54 int32_t A::GetTaskArg(IMsgBody *pMsgBody,Arg *stArg)
55 {
56     GetTaskArg(pMsgBody,stArg,&A::GetArg_GameEnd);
57     return 0;
58 }

 

posted @ 2015-09-19 17:12  你好阿汤哥  Views(2521)  Comments(0Edit  收藏  举报