473,412 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

returning map value in const function

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
3 2449
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
ÓÚ 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;
thanks

shaun


Jul 23 '05 #3
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jochen Zeischka | last post by:
I'm puzzled. When compiling this: template<class ValRes, class Val1, class Val2> Veld<ValRes>& mult(Veld<ValRes>& res, const Veld<Val1>& v1, const Veld<Val2>& v2) { // something return res; }...
18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
19
by: JKop | last post by:
When I compile and run the following on my system: #include <iostream> static int hello = 78; int ReturnValue(void) {
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
11
by: JKop | last post by:
AnyClass Blah() { AnyClass poo; return poo; } As we all know, in the above, the compiler is entitled to:
4
by: Siemel Naran | last post by:
Hi. I have found one advantage of returning values through the argument list. It's that we have to store the return value. But when we return by value, we may forgot to store the return value. ...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
13
by: Matthias Kaeppler | last post by:
Hi, I was wondering why library implementors often make getter functions return strings by value (copies). For example, in boost::filesystem the leaf() function returns an std::string by value....
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.