简体   繁体   中英

can't access const static std::map enum struct

I am trying to have a enum struct mapping to guarante a default value to a config value if it does not exsist and to guarante only access to "real" config values.(No std::string get)

So the Header looks like this:

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;
}

And the cpp contains this:

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
}

But i cant access the map.

operator[] is not const for a std::map .

Use at instead.

Before C++11 :

If you are not using C++11, you have to use find which has a const version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM