2016-12-24 83 views
1

有没有办法将std :: map索引作为参数传递给const函数?在const函数内通过参数索引返回std :: map的值

const SurfaceProperties & SurfacePropertiesImpl::getSurfaceProperties(const std::string entityId) const 
{ 
    return mSurfaceProperties[entityId]; 
} 

-

error: passing ‘const std::map<std::__cxx11::basic_string<char>, SurfaceProperties>’ as ‘this’ argument discards qualifiers [-fpermissive] 
    return mSurfaceProperties[entityId]; 

回答

2

您可以使用at是常量违背operator[]

const SurfaceProperties& 
SurfacePropertiesImpl::getSurfaceProperties(const std::string entityId) const 
{ 
    return mSurfaceProperties.at(entityId); 
} 

如果关键是没有找到它会抛出。

相关问题