Connecting Tech Pros Worldwide Forums | Help | Site Map

[STL] [Maps] operator[]

r.simoni
Guest
 
Posts: n/a
#1: Oct 22 '06
Hi, i have seen that this operator returns a reference to TYPE and not
a const reference.

I have a problem and I can't solve it:
i have a properties class with a get_property(const string &) function
that is const due to the fact that this method doesn't change the
"content" of properties class. If I write the method as this:
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?
Thanks to all
Bye

--
Roberto Simoni


Ron Natalie
Guest
 
Posts: n/a
#2: Oct 22 '06

re: [STL] [Maps] operator[]


r.simoni wrote:
Quote:
>
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?
Thanks to all
There is no map operator[] that is const. The problem is the
definition of the operator modifies the map if there isn't
already a pair in the map that matches.

It's clunky but you can do:

const string& get_property(const string& key) const {
map::<string, string>::conost_iterator it = all_props.find(key);
if(it != all_props.end())
return (*it).second;
else
return string();
}
Clark S. Cox III
Guest
 
Posts: n/a
#3: Oct 22 '06

re: [STL] [Maps] operator[]


r.simoni wrote:
Quote:
Hi, i have seen that this operator returns a reference to TYPE and not
a const reference.
>
I have a problem and I can't solve it:
i have a properties class with a get_property(const string &) function
that is const due to the fact that this method doesn't change the
"content" of properties class. If I write the method as this:
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?
Your problem is that std::map doesn't have a const operator[]. The way
that operator[] is defined by the standard can modify the map itself.
Quote:
Is there
another way for coding this?
You need to use the find() function:

const string& get_property(const string& key) const
{
std::map<string,string>::const_iterator i = all_prop.find(key);
return (i == all_prop.end())?string():i->second;
}

--
Clark S. Cox III
clarkcox3@gmail.com
LR
Guest
 
Posts: n/a
#4: Oct 22 '06

re: [STL] [Maps] operator[]


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
LR
Guest
 
Posts: n/a
#5: Oct 22 '06

re: [STL] [Maps] operator[]


Ron Natalie wrote:

Quote:
const string& get_property(const string& key) const {
map::<string, string>::conost_iterator it = all_props.find(key);
if(it != all_props.end())
return (*it).second;
else
return string();
^^^^^^^^
Can you please tell me what the life time of the temp object in the
above return statement is?

TIA
Quote:
}
LR
Ron Natalie
Guest
 
Posts: n/a
#6: Oct 22 '06

re: [STL] [Maps] operator[]


LR wrote:
Quote:
Ron Natalie wrote:
>
>
Quote:
> const string& get_property(const string& key) const {
> map::<string, string>::conost_iterator it = all_props.find(key);
> if(it != all_props.end())
> return (*it).second;
> else
> return string();
^^^^^^^^
Can you please tell me what the life time of the temp object in the
above return statement is?
>
>
Oops, you're right. I forgot the thing was a returning
a reference. Bad thing. I originally just had a comment
there since it wasn't clear what the original poster wanted
to do in that case.

r.simoni
Guest
 
Posts: n/a
#7: Oct 23 '06

re: [STL] [Maps] operator[]



Ron Natalie ha scritto:
Quote:
LR wrote:
Quote:
Ron Natalie wrote:

Quote:
const string& get_property(const string& key) const {
map::<string, string>::conost_iterator it = all_props.find(key);
if(it != all_props.end())
return (*it).second;
else
return string();
^^^^^^^^
Can you please tell me what the life time of the temp object in the
above return statement is?
Oops, you're right. I forgot the thing was a returning
a reference. Bad thing. I originally just had a comment
there since it wasn't clear what the original poster wanted
to do in that case.
Thanks to all.
The problem is that in my book, I don't have a well organized section
for STL. So i don't remember that to use a map in "constant mode" i
have to use a const_iterator
Thanks

Closed Thread