C++文字列に基づいて同名関数を呼び出す


文字列に基づいて、文字列と同じ名前の関数を呼び出します.
#include 
#include 
#include 

typedef void (*pFunc)();
std::map<:string pfunc=""> strFuncMap;

void func1()
{
	printf("this is func1!
"); } void func2() { printf("this is func2!
"); } void buildMap() { strFuncMap["func1"] = &func1; strFuncMap["func2"] = &func2; } void callFunc(const std::string& str) { if(strFuncMap.count(str)) (*strFuncMap[str])(); else std::cout << "unsupported function str : " << str.c_str() << "
" << std::endl; } int main() { buildMap(); //begin call func callFunc("func1"); callFunc("func2"); callFunc("func3"); system("pause"); return 0; }

出力結果:
this is func1! this is func2! unsupported function str : func3