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?
It will point to the new map, as you copied the map into variable
min the ctor of the class. The statementm(newM)in the initialization list invokes the copy constructor of thestd::mapclass and copies individual elements of the passed map into the destintation mapm. Hence when you invokebarmethod, it will return the iterator from this new map.EDIT
Example code for storing the
std::mapas reference: