returning map value in const function

S

shaun

I have a function which does not modify a private data member which is a
map, but simply returns one value.
I'd like to make that member function const, like so:

int MapTest:: getValueAt(const int keyVal) const {

return m_myMap[keyVal];

}

BUT I get a compiler warning, I guess because the m_myMap[keyVal] has
the capability to alter m_myMap.
What is the accepted way to access the map and return a value, ensuring
that the map is not altered so the 'get' function can be const?

thanks

shaun
 
R

Rolf Magnus

shaun said:
I have a function which does not modify a private data member which is a
map, but simply returns one value.
I'd like to make that member function const, like so:

int MapTest:: getValueAt(const int keyVal) const {

return m_myMap[keyVal];

}

BUT I get a compiler warning,

It probably does say more than just "warning", doesn't it? What's the exact
message?
I guess because the m_myMap[keyVal] has the capability to alter m_myMap.

Yes. The compiler should actually bail out with an error message instead of
just giving a warning.
What is the accepted way to access the map and return a value, ensuring
that the map is not altered so the 'get' function can be const?

The thing with operator[] is that - if it doesn't find the element with the
specified key - it inserts it into the map. Therefore, it isn't const.
So you have to use a function that doesn't insert elements - in this case,
that would be find():

int MapTest:: getValueAt(const int keyVal) const
{
std::map<int, int>::const_iterator it = m_myMap.find(keyVal);
if (it != m_myMap.end())
return (*it).second;
else
//do whatever should happen if the value is not found
}
 
J

jame

ÓÚ Mon, 18 Apr 2005 12:22:10 +0200£¬shaunдµ½£º
I have a function which does not modify a private data member which is a
map, but simply returns one value.
I'd like to make that member function const, like so:

int MapTest:: getValueAt(const int keyVal) const {

return m_myMap[keyVal];

}

BUT I get a compiler warning, I guess because the m_myMap[keyVal] has
the capability to alter m_myMap.
What is the accepted way to access the map and return a value, ensuring
that the map is not altered so the 'get' function can be const?
it = m_myMap.find(...);
return *it->second;
 
S

shaun

Many thanks. In fact the compiler on OS X gives me a warning but runs,
whereas the compiler on linux bombs. It is the same underlying gcc
version, so I guess that my build system is turning various errors off
in the compiler options. Anyway, your answer solves my problem.
cheers

shaun

Rolf Magnus said:
shaun said:
I have a function which does not modify a private data member which is a
map, but simply returns one value.
I'd like to make that member function const, like so:

int MapTest:: getValueAt(const int keyVal) const {

return m_myMap[keyVal];

}

BUT I get a compiler warning,

It probably does say more than just "warning", doesn't it? What's the exact
message?
I guess because the m_myMap[keyVal] has the capability to alter m_myMap.

Yes. The compiler should actually bail out with an error message instead of
just giving a warning.
What is the accepted way to access the map and return a value, ensuring
that the map is not altered so the 'get' function can be const?

The thing with operator[] is that - if it doesn't find the element with the
specified key - it inserts it into the map. Therefore, it isn't const.
So you have to use a function that doesn't insert elements - in this case,
that would be find():

int MapTest:: getValueAt(const int keyVal) const
{
std::map<int, int>::const_iterator it = m_myMap.find(keyVal);
if (it != m_myMap.end())
return (*it).second;
else
//do whatever should happen if the value is not found
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top