By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,015 Members | 1,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,015 IT Pros & Developers. It's quick & easy.

returning map value in const function

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
shaun wrote:
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
}

Jul 23 '05 #2

P: n/a
Mon, 18 Apr 2005 12:22:10 +0200shaunд
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;
thanks

shaun


Jul 23 '05 #3

P: n/a
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

In article <d4*************@news.t-online.com>,
Rolf Magnus <ra******@t-online.de> wrote:
shaun wrote:
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
}

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.