2016-12-24 87 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); 
} 

如果關鍵是沒有找到它會拋出。

相關問題