我试图有一个枚举结构映射,以保证默认值配置值,如果它不存在,并保证只访问"真实"配置值。(无std::string get)
所以Header看起来像这样:
enum ConfigValues
{
LOG_LEVEL,
};
class Config
{
public:
std::string get(const ConfigValues& key);
private:
struct ConfigMapping
{
std::string configKeyString;
std::string defaultValue;
};
const static std::map<ConfigValues, ConfigMapping> m_mapping;
}
cpp包含如下内容:
const std::map<ConfigValues, Config::ConfigMapping> Config::m_mapping=
{
{LOG_LEVEL, { "logLevel", "5" } },
};
std::string Config::get(const ConfigValues& key)
{
std::string key = m_mapping[key].configKeyString; // <-- Does not work
}
但是我不能访问地图
operator[]
是不是对于std::map
的const
用at
代替
c++ 11之前:
如果你不使用c++ 11,你必须使用find
,它有一个const版本。