あなただけの同じ引数で、同じクラスのメンバーを格納し、型を返す、あなたはポインタ・ツー・メンバー関数を使用することができますする必要がありますので:
bool foo::call(char const * name) const {
static std::map<std::string, bool (foo::*)() const> table
{
{"one", &foo::one},
{"two", &foo::two}
};
auto entry = table.find(name);
if (entry != table.end()) {
return (this->*(entry->second))();
} else {
return false;
}
}
Cの新しい初期化構文を使用しています++ 11。あなたのコンパイラがそれをサポートしていない場合は、さまざまなオプションがあります。
typedef std::map<std::string, bool (foo::*)() const> table_type;
static table_type table = make_table();
static table_type make_table() {
table_type table;
table["one"] = &foo::one;
table["two"] = &foo::two;
return table;
}
か、Boost.Assignment使用できます:あなたは、静的な機能とマップを初期化することができ
static std::map<std::string, bool (foo::*)() const> table =
boost::assign::map_list_of
("one", &foo::one)
("two", &foo::two);
をしたり、配列を使用することができ、およびstd::find_if
を持つエントリ(または単純for
ループの場合を見つけますあなたのライブラリにはそれがまだありません)、または配列がソートされていることを確認する場合はstd::binary_search
です。
これはすべて非常に手作業です。より興味深いのは、自動的に行うための最良の方法です。 – Dan