r.simoni wrote:
Quote:
const string& get_property(const string& key) const {
return all_prop[key];
}
>
i receive an error in compile. The only way I have found to solve this
is to define class member all_prop as mutable. Is it correct? Is there
another way for coding this?
// asumming that...
typedef std::map<std::string,std::string AllPropertiesMap;
// and
AllPropertiesMap all_prop;
// then
const std::string &get_property(const std::string &key) const {
AllPropertiesMap::const_iterator i = all_prop.find(key);
if(i != all_prop.end()) return i->second;
//
// here you have some options.
// you could return a const static local
// you could throw
// maybe something else
}
Generally, I think that I would prefer:
static const std::string &bad_property() {
static const std::string bp = "Bad Property";
return bp;
}
const std::string &get_property(const std::string &key) const {
AllPropertiesMap::const_iterator i = all_prop.find(key);
return i != all_prop.end() ? i->second : bad_property();
}
But undoubtedly, YMWV according to the requirements of your application.
HTH
LR