Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion operator[] in std::map
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
bartek szurgot  
View profile  
 More options Nov 12 2012, 12:00 pm
Newsgroups: comp.lang.c++
From: bartek szurgot <b...@no.spam>
Date: Mon, 12 Nov 2012 18:00:31 +0100
Local: Mon, Nov 12 2012 12:00 pm
Subject: Re: operator[] in std::map
On 11/11/2012 10:23 PM, Ian Collins wrote:

> On 11/12/12 10:20, Fab wrote:
>> Dear all

>> I use std::map for some application.  I would like to return a const
>> reference to some value with the operator[] function.  I have a public
>> method of the following form:

>> const value&  getValue( const int i ) const
>> {
>>          return myMap[i];
>> }

>> where myMap is of type std::map.  When I compile this with gcc, I get an
>> error due to the const method qualifier.  I can't figure out why?

> Map's operator[] isn't const.  It will modify the map (by inserting a
> default value) if the element does not exist.

indeed. there is one more drawback - operator[] requires that "value" is
default constructable (because of reasons described above).

--
pozdrawiam serdecznie / best regards,
bartek szurgot
/* http://www.baszerr.eu */


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.