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 }
  • 相关阅读:
    Istio安装配置及使用
    Istio介绍
    Rancher管理k8s集群
    EFK部署
    常见日志收集方案及相关组件
    Prometheus Pushgateway
    Prometheus监控拓展
    Prometheus PromQL语法
    开始新工作了
    SpringBlade 新系统 运行
  • 原文地址:https://www.cnblogs.com/tangxin-blog/p/4821816.html
Copyright © 2011-2022 走看看