stl map question

T

Thomas

I have a question about how the stl map class works. I have a subscription
class that I use to manage client application subscriptions. These are
stored in a map. Periodically, I need to update the subscriptions in the map
to reflect changes to data. When I iterate over the map, I expect to get a
reference to the object stored in the map using

myclass mc = *iter->second;

What I am finding is that the copy constructor is getting invoked resulting
in a copy getting returned instead of a reference.
I tried the alternate approach using the operator[] but get the same
results.

At this point the workaround is to put the modified object back into the map
which seems terribly inefficient.

I checked the SGI website and it looks like it should be returning a
reference and not a copy. But I am not certain.

This is on Solaris 2.8 with 6.2 compiler.

Does this sound correct? Should I expect this to be the defined behavior or
am I missing something?

Thanks,

Thomas
 
B

Buster Copley

Thomas said:
I have a question about how the stl map class works. I have a subscription
class that I use to manage client application subscriptions. These are
stored in a map. Periodically, I need to update the subscriptions in the map
to reflect changes to data. When I iterate over the map, I expect to get a
reference to the object stored in the map using

myclass mc = *iter->second;
[snip]

C++ is not Java. This declares an object of class mc and copy-
initializes it from the reference returned by "* (iter->second)".

If you want "mc" to be a reference to a myclass object, declare
it as such:

myclass & mc = * (iter->second);

Regards,
Buster.
 
F

Frank Schmitt

Buster Copley said:
Thomas said:
I have a question about how the stl map class works. I have a subscription
class that I use to manage client application subscriptions. These are
stored in a map. Periodically, I need to update the subscriptions
in the map
to reflect changes to data. When I iterate over the map, I expect to get a
reference to the object stored in the map using
myclass mc = *iter->second;
[snip]

C++ is not Java. This declares an object of class mc and copy-
initializes it from the reference returned by "* (iter->second)".

If you want "mc" to be a reference to a myclass object, declare
it as such:

myclass & mc = * (iter->second);

Alternatively, since you seem to store pointers to myclass in the map, just
use pointers:

myclass* mc = iter->second;

regards
frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Sorting an STL map 1
Placing a google map into my code 5
STL Map Problem 3
STL map insert Options 8
STL list or map? 33
STL map and char * problems 3
Modify STL map Object 4
STL::MAP: Printing values only once .. 9

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top