Topic: question about stl map
Author: tdineen@erols.com ("Thomas")
Date: Fri, 26 Sep 2003 17:33:51 +0000 (UTC) Raw View
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 the myclass mc =
*iter->second notation;. 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
is there another approach?
Thanks,
Thomas
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: bolo@coco.ro ("Florian Preknya")
Date: Mon, 6 Oct 2003 15:10:03 +0000 (UTC) Raw View
If you use
myclass mc = *iter->second;
the copy constructor is invoked and mc will be a copy of the stored element.
You want to modify the element in the map, not the copy it, so you must get
a reference of that element. The correct version is:
myclass& mc = *iter->second;
Now the modification on mc will affect the element in the map the iterator
points to.
Bolo.
""Thomas"" <tdineen@erols.com> wrote in message
news:bl0bca$k2r$1@bob.news.rcn.net...
> 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 the myclass mc =
> *iter->second notation;. 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
> is there another approach?
>
> Thanks,
>
> Thomas
>
>
>
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: philippe_mori@hotmail.com ("Philippe Mori")
Date: Mon, 29 Sep 2003 05:08:46 +0000 (UTC) Raw View
> 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 the myclass mc =
> *iter->second notation;. 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.
>
You should write myclass &mc = iter->second;
That is you want a reference on left side... Also on the right side
iter->second already returns a reference to its member so you should
not need an * (except if you are storing pointers).
Philippe
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: wolof@freemail.hu ("WW")
Date: Mon, 29 Sep 2003 05:10:19 +0000 (UTC) Raw View
"Thomas" wrote:
> 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 the myclass mc = *iter->second notation;.
> 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.
[SNIP]
Rerefence? So why don't you ask for one?
myclass &mc = *iter->second notation;
^
|
--------+
Now it is a reference.
--
WW aka Attila
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: dhruvbird@gmx.net ("Dhruv")
Date: Mon, 29 Sep 2003 05:13:42 +0000 (UTC) Raw View
On Fri, 26 Sep 2003 17:33:51 +0000, Thomas wrote:
> 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 the myclass mc =
> *iter->second notation;. 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.
>
If you do mc = (*iterator)->second, then the copy ctor will be invoked,
but if you directly assign to it without the mc = part, then it's fine,
something like: *iterator->second = <something>. Here, only the assignment
operator will be invoked.
> 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
> is there another approach?
Regards,
-Dhruv.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: jpotter@falcon.lhup.edu (John Potter)
Date: Mon, 29 Sep 2003 05:15:44 +0000 (UTC) Raw View
On Fri, 26 Sep 2003 17:33:51 +0000 (UTC), tdineen@erols.com ("Thomas")
wrote:
> myclass mc = iter->second notation;
I removed a * in the above.
> What I am finding is that the copy constructor is
> getting invoked resulting in a copy getting returned instead of a reference.
You asked for a copy.
myclass& mc(iter->second);
Now you have a reference.
John
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: wbruna@yahoo.com (Wagner Bruna)
Date: Mon, 29 Sep 2003 17:08:05 +0000 (UTC) Raw View
Hello,
tdineen@erols.com ("Thomas") wrote in message news:<bl0bca$k2r$1@bob.news.rcn.net>...
> 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 the myclass mc =
> *iter->second notation;. What I am finding is that the copy constructor is
> getting invoked
I think your map is declared as
std::map < Something, myclass >
so you should write
myclass mc = iter->second
And if your class doesn't have an assignment operator, '=' calls the
copy constructor. Try using a reference instead:
myclass & mc = iter->second;
Hope that helps.
Wagner
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]