07/09/28 15:12:10
一般的じゃないかもしれないけどこういう手が楽っぽい
#include <map>
#include <string>
#include <iostream>
void funcA() { std::cout << "invoke func A\n" ; }
void funcB() { std::cout << "invoke func B\n" ; }
int main(void) {
std::map< std::string, void(*)(void) > funcs ;
funcs[ "あいうえお" ] = funcA ;
funcs[ "かきこくけこ" ] = funcB ;
std::string str = "あいうえお" ;
void(*f)(void) = 0;
f = funcs[str];
if ( !f )
// default: の処理
else
f();
return 0;
}
要は条件をキー、呼び出す関数を値にするmapだな
値をboost::functionへの参照とかにしてやれば、動的なオブジェクトのメソッドを呼び出すことも可能