zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    ngnix-内网能用,外网不能用
    python学习
    mysql数据库导出xls-自定义
    Oralce-资源配置PROFILE
    oracle-用户管理与权限分配
    Oracle-创建索引分区
    Oracle-表分区
    Oracle--索引视图序列等对象
    Oracle-数据表对象
    Oracle-管理表空间和数据文件
  • 原文地址:https://www.cnblogs.com/tangxin-blog/p/4821816.html
Copyright © 2011-2022 走看看