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 8057521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:03:42+00:00 2026-06-05T09:03:42+00:00

I am trying to initialize a STL map using C++11 syntax but that doesn’t

  • 0

I am trying to initialize a STL map using C++11 syntax but that doesn’t seem to work. After initialization, when i try to access the element, it tries to call the private constructor of Foo. Did I miss something? It works if I use at. I am wondering if I could use operator[] to access initialized values…

#include <map>
#include <string>

class Foo{
public:
    int a, b;
    Foo(int a_, int b_){
        a = a_;
        b = b_;
    }

private:
    Foo(){};
};


int main(){

    std::map<std::string, Foo> myMap = { {"1", Foo(10,5)}, {"2", Foo(5,10)} };
    int b  = myMap["1"].b;    // it tries to call private constructor of Foo.
    return 0;
}
  • 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-06-05T09:03:43+00:00Added an answer on June 5, 2026 at 9:03 am

    When you use the operator[] on a map, you may use the operator to either get a value from the map or assign a value into the map. In order to assign the value into the map, the map has to construct an object of its value type, and return it by reference, so that you can use operator= to overwrite an existing object.

    Consequently, the type has to be default constructible so that a new object can be created for you to assign into.

    At run time, the constructor won’t be called if the key already exists, but the compiler has no way of knowing whether you’ll ever use operator[] to access a value that doesn’t exist, so it requires the constructor to be public.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to use the stl map with a class that i've created, from
I'm trying to initialize an array of structures to all-0's, using the below syntax:
I've been trying to initialize a map of <ints, vector<ints> > using the new
I'm trying to initialize a class attribute within a php constructor method, but am
I am trying to initialize data in index function of controller, so that initialized
I have been trying to initialize 1 vector and 1 map in a class.
I am trying to initialize NSDictionary but it gives me following error: Program received
I am trying to initialize an NSMutableArray with the values of an NSArray but
I'm trying to initialize SecureString and I need Char* for that. I tried to
I'm trying to initialize a dict variable but I don't understand why one way

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.