zoukankan      html  css  js  c++  java
  • [C/C++]函数指针和函数分发表

    // console.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    typedef unsigned char UCHAR;
    int FunA();
    int FunB();
    int FunC();
    
    typedef enum tagMsgType
    {
        MSG_TYPE_A = 1,
        MSG_TYPE_B,
        MSG_TYPE_C
    };
    
    typedef int (*MSG_PROC_FUNC)();
    
    typedef struct tagMsgDispatchTbl
    {
        UCHAR ucMsgType;
        MSG_PROC_FUNC pFuc;
    } MsgDispatchTbl;
    
    MsgDispatchTbl g_astDispatchTbl[] = 
    {
        {MSG_TYPE_A, FunA},
        {MSG_TYPE_B, FunB},
        {MSG_TYPE_C, FunC},
    };
    
    int FunA()
    {
        printf("Call FunA
    ");
        return 0;
    }
    
    int FunB()
    {
        printf("Call FunB
    ");
        return 0;
    }
    
    int FunC()
    {
        printf("Call FunC
    ");
        return 0;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int i = 1;
        int iRet = 0;
        UCHAR ucMsgType = MSG_TYPE_B;
        UCHAR aucStr[1024] = {0};
        MSG_PROC_FUNC pFunc;
        for (i = 1; i < 4; i++)
        {
            if (ucMsgType == g_astDispatchTbl[i].ucMsgType)
            {
                pFunc = g_astDispatchTbl[i].pFuc;
                pFunc();
            }
            
        }
        return 0;
    }
  • 相关阅读:
    Friends ZOJ
    2^x mod n = 1 HDU
    Paint the Grid Reloaded ZOJ
    Treap 模板
    bzoj进度条
    。。。
    bzoj
    。。。
    bzoj
    题解continue
  • 原文地址:https://www.cnblogs.com/jingmoxukong/p/3398245.html
Copyright © 2011-2022 走看看