编辑代码

#include <stdio.h>
#include <cstring>

static constexpr int kFuncNoDumpStart  = 0;
static constexpr int kFuncNoDumpFund   = 1;
static constexpr int kFuncNoDumpHold   = 2;
static constexpr int kFuncNoDumpSec    = 3;
static constexpr int kFuncNoDumpOrder  = 4;
static constexpr int kFuncNoChangeFund = 11;
static constexpr int kFuncNoChangeHold = 12;
static constexpr int kFuncNoDumpEnd    = 99;


void DumpFundHandlers() {
    printf("I am DumpFundHandlers...\n");
}

void DumpHoldHandlers() {
    printf("I am DumpHoldHandlers...\n");
}

//========== Test1 ==========
#define CREATE_DUMPTAG_HANDLER(NAME) {#NAME, NAME##Handlers}

struct FuncHandlerBase {
    const char* name;
    void (*function) (void);
};

void Test1()
{
    struct FuncHandlerBase handlers[kFuncNoDumpEnd];
    memset(&handlers, 0, sizeof(handlers));
    handlers[kFuncNoDumpFund] = CREATE_DUMPTAG_HANDLER(DumpFund);
    handlers[kFuncNoDumpHold] = CREATE_DUMPTAG_HANDLER(DumpHold);

    handlers[kFuncNoDumpFund].function();
    handlers[kFuncNoDumpHold].function();

    auto& funcHandler = handlers[kFuncNoDumpSec].function;
    if (!funcHandler)
    {
        printf("kFuncNoDumpSec Not Invalid\n");
    }
}
//========== Test1 ==========

//========== Test2 ==========
using FuncHandlerPtr = void (*)();

void Test2()
{
    FuncHandlerPtr handlers[kFuncNoDumpEnd];
    memset(&handlers, 0, sizeof(handlers));
    handlers[kFuncNoDumpFund] = &DumpFundHandlers;
    handlers[kFuncNoDumpHold] = &DumpHoldHandlers;

    handlers[kFuncNoDumpFund]();
    handlers[kFuncNoDumpHold]();

    auto& funcHandler = handlers[kFuncNoDumpSec];
    if (!funcHandler)
    {
        printf("kFuncNoDumpSec Not Invalid\n");
    }
}
//========== Test2 ==========

int main() {
    Test1();
    Test2();
    return 0;
}