地图初始化中的C ++地图

时间:2014-11-03 19:00:20

标签: c++ initializer-list

我有以下定义的地图:

map<string, Command> _cmd_map;

其中Command是定义为

的类
class Command {
public:

    const string description;
    const map<string, Argument> *arguments;
    const CommandFunction function;

    Command(const string &desc, CommandFunction func, map<string, Argument> *args = nullptr);
    Command(const string &desc, CommandFunction func, const map<string, Argument> &args);
};

如何在我的.cpp开头初始化它?到目前为止,我可以使用commandizer_list for command,* args = nullptr:

const map<string, Command> _cmd_map{
    { "help", Command("Show help menu", (CommandFunction)&(_cmd_help)) },
    { "exit", Command("Exit ", (CommandFunction)&(_cmd_exit)) },
    { "quit", Command("Exit ", (CommandFunction)&(_cmd_exit)) },
}

但是我无法以这种方式初始化Command类中的映射,而无需先创建映射并将其作为参数传递。 是否可以直接执行此操作,例如

const map<string, Command> _cmd_map{
{ "quit", Command("Exit ", (CommandFunction)&(_cmd_exit)), 
    {
        {"d1", Argument("Dummy Argument 1")},
        {"d2", Argument("Dummy Argument 2")},
     } 
}
}

0 个答案:

没有答案