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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:12:29+00:00 2026-05-16T01:12:29+00:00

I have a class with a map<K,V> variable which gets its value in the

  • 0

I have a class with a map<K,V> variable which gets its value in the c’tor like so:

class Foo {
    map<K,V> m;
    Foo (map<K,V>& newM) : m(newM) {}
    map<K,V>::iterator bar () { ... }
}

the function bar iterates through the map m, and return some an iterator to some element. I’m calling the function like this:

std::map<K,V> map;
//fill map
Foo foo(map);
map<K,V>::iterator it = foo.bar();

my question is, at this moment, does it point to a member of map? or was it copied to Foo.m and therefor the iterator points to a different map?

  • 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-16T01:12:29+00:00Added an answer on May 16, 2026 at 1:12 am

    It will point to the new map, as you copied the map into variable m in the ctor of the class. The statement m(newM) in the initialization list invokes the copy constructor of the std::map class and copies individual elements of the passed map into the destintation map m. Hence when you invoke bar method, it will return the iterator from this new map.

    EDIT
    Example code for storing the std::map as reference:

    class Foo {
    public:
        map<int,int>& m; //Note: the change here, I am storing a reference
        Foo (map<int,int>& newM) : m(newM) {}
        map<int,int>::iterator bar () { return m.begin();}
    };
    
    
    int main()
    {
        std::map<int,int> map1;
        Foo foo(map1);
        map<int,int>::iterator it = foo.bar();
    
        if(it == map1.begin())
        {
            std::cout<<"Iterators are equal\n";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like the one below: class Foo { private: std::map<std::string, Bar*>
I have this class which i would like to map: public class Contract {
I have an unordered map: class O(val a: Int) Map[String, List[O]] which I'd like
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I have a CustomerRelation class which has a method getInstance() and instance variable i.e
I have class that looks like this: class A { public: class variables_map vm
I have a class with a std::map used as a container. I want to
I have a php class that generates a map image depending on my db
In my action class I have a Map<Long, Set<String>> attribute, named accountsMap. Lets say
Using Dozer to map two objects, I have: /** /* This first class uses

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.