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

时间:2022-05-14 07:37:09
// 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 = ,
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 ;
} int FunB()
{
printf("Call FunB\r\n");
return ;
} int FunC()
{
printf("Call FunC\r\n");
return ;
} int _tmain(int argc, _TCHAR* argv[])
{
int i = ;
int iRet = ;
UCHAR ucMsgType = MSG_TYPE_B;
UCHAR aucStr[] = {};
MSG_PROC_FUNC pFunc;
for (i = ; i < ; i++)
{
if (ucMsgType == g_astDispatchTbl[i].ucMsgType)
{
pFunc = g_astDispatchTbl[i].pFuc;
pFunc();
} }
return ;
}