且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

[C/C++]函数指针和函数分发表

更新时间:2022-05-25 19:57:41

 

[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\r\n");
    return 0;
}

int FunB()
{
    printf("Call FunB\r\n");
    return 0;
}

int FunC()
{
    printf("Call FunC\r\n");
    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;
}
[C/C++]函数指针和函数分发表

 

本文转自静默虚空博客园博客,原文链接:http://www.cnblogs.com/jingmoxukong/p/3398245.html,如需转载请自行联系原作者