Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6238091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:08:55+00:00 2026-05-24T11:08:55+00:00

I was surprised when the declaration: const std::map<double, Foo*> myCollection; would not allow me

  • 0

I was surprised when the declaration:

const std::map<double, Foo*> myCollection;

would not allow me to call non-const methods on Foo*. I thought the const applied to the collection itself (i.e., no items could be added or removed). I would think the way to convey item const would be:

std::map<double, Foo const*> myCollection;

It seems the const in the first code snippet is distributive to the contents of the map instead of my assumption of applying only to the collection.

Am I missing something here? It seems counter-intuitive. How can I call non-const methods on the Foo* objects in the first declaration?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:08:57+00:00Added an answer on May 24, 2026 at 11:08 am

    You must be doing something wrong — operator[] on std::map is not declared const, so it cannot be used to access elements of the map. The reason it’s not const is because if the key isn’t present in the map, it gets added to the map with a default-constructed value, and doing so modifies the map.

    Instead, use the find method to get an iterator. The returned iterator cannot be used to modify the map, but it can be used to modify the objects which are pointed to by the values. For example, the following code compiles and outputs 4:

    #include <iostream>
    #include <map>
    
    typedef std::map<int, int*> Map;
    
    void foo(const Map& m)
    {
      Map::const_iterator i = m.find(0);
      (*i->second)++;
    }
    
    int main(void)
    {
      Map x;
      int y = 3;
      x[0] = &y;
      foo(x);
    
      std::cout << *x[0] << std::endl;
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am surprised not to find it in 2010, I would be very sad
I am surprised I could not find a "batch" function that would take as
I would not be surprised if this is not possible, but I need to
I'm a little surprised that it is not possible to set up a binding
I'm surprised this doesn't happen automatically, but I would like my applications window to
I was pretty surprised when i saw http://www.vorbis.com/music/Hydrate-Kenny_Beltrey.ogg link not give me a download
I was surprised not to find an answer to this question, maybe is something
I was surprised to learn that ref and out parameters are not marked by
I was quite surprised when [] is not [] evaluated to True . What
I'm surprised that I could not find any info on this. I must be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.