stl map question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas

    stl map question

    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





  • Buster Copley

    #2
    Re: stl map question

    Thomas wrote:[color=blue]
    > 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;[/color]
    [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.

    Comment

    • Frank Schmitt

      #3
      Re: stl map question

      Buster Copley <buster@none.co m> writes:
      [color=blue]
      > Thomas wrote:[color=green]
      > > 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;[/color]
      > [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);[/color]

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

      myclass* mc = iter->second;

      regards
      frank

      --
      Frank Schmitt
      4SC AG phone: +49 89 700763-0
      e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com

      Comment

      Working...