map::erase

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

    map::erase

    I have a map<int, obj>. When I do a map::erase(iter ator), does this call the
    destructor of the object? Or does it just remove that entry from the map.


  • Victor Bazarov

    #2
    Re: map::erase

    Viral Shah wrote:[color=blue]
    > I have a map<int, obj>. When I do a map::erase(iter ator), does this call the
    > destructor of the object? Or does it just remove that entry from the map.[/color]

    The entry in the map _contains_ the object. Of course while removing the
    entry, it _has_to_ destroy the object.

    V

    Comment

    • John Dibling

      #3
      Re: map::erase

      Viral Shah wrote:[color=blue]
      > I have a map<int, obj>. When I do a map::erase(iter ator), does this call the
      > destructor of the object? Or does it just remove that entry from the map.[/color]

      It calls the destructor. A note: if the map is actually a map of
      pointers to obj's and not actually obj's themselves, then what is
      destroyed is the pointer to the obj - not the obj itself. In other
      words, erase doesn't call 'delete' for you if the map is declared like
      this: 'map<int, obj*>'.

      Take care,

      John Dibling

      Comment

      • verec

        #4
        Re: map::erase

        On 2005-06-21 21:46:53 +0100, "John Dibling" <jdibling@gmail .com> said:
        [color=blue]
        > Viral Shah wrote:[color=green]
        >> I have a map<int, obj>. When I do a map::erase(iter ator), does this call the
        >> destructor of the object? Or does it just remove that entry from the map.[/color]
        >
        > It calls the destructor. A note: if the map is actually a map of
        > pointers to obj's and not actually obj's themselves, then what is
        > destroyed is the pointer to the obj - not the obj itself. In other
        > words, erase doesn't call 'delete' for you if the map is declared like
        > this: 'map<int, obj*>'.[/color]

        But it *does* call delete if your map is of the form map<int, smart_ptr<T> >
        for some suitable definition of smart_ptr<T>

        I know: I'm using this all the time :-)
        --
        JFB

        Comment

        • Victor Bazarov

          #5
          Re: map::erase

          verec wrote:[color=blue]
          > On 2005-06-21 21:46:53 +0100, "John Dibling" <jdibling@gmail .com>
          > said:[color=green]
          >> Viral Shah wrote:[color=darkred]
          >>> I have a map<int, obj>. When I do a map::erase(iter ator), does this
          >>> call the destructor of the object? Or does it just remove that
          >>> entry from the map.[/color]
          >>
          >> It calls the destructor. A note: if the map is actually a map of
          >> pointers to obj's and not actually obj's themselves, then what is
          >> destroyed is the pointer to the obj - not the obj itself. In other
          >> words, erase doesn't call 'delete' for you if the map is declared
          >> like this: 'map<int, obj*>'.[/color]
          >
          > But it *does* call delete if your map is of the form map<int,
          > smart_ptr<T> > for some suitable definition of smart_ptr<T>
          >
          > I know: I'm using this all the time :-)[/color]

          But that's not the feature of the map. It's the destruction of the
          'smart_ptr<T>' object what does the 'delete'ion.

          V


          Comment

          • verec

            #6
            Re: map::erase

            On 2005-06-22 02:36:42 +0100, "Victor Bazarov" <v.Abazarov@com Acast.net> said:
            [color=blue]
            > verec wrote:[color=green]
            >> On 2005-06-21 21:46:53 +0100, "John Dibling" <jdibling@gmail .com>
            >> said:[color=darkred]
            >>> Viral Shah wrote:
            >>>> I have a map<int, obj>. When I do a map::erase(iter ator), does this
            >>>> call the destructor of the object? Or does it just remove that
            >>>> entry from the map.
            >>>
            >>> It calls the destructor. A note: if the map is actually a map of
            >>> pointers to obj's and not actually obj's themselves, then what is
            >>> destroyed is the pointer to the obj - not the obj itself. In other
            >>> words, erase doesn't call 'delete' for you if the map is declared
            >>> like this: 'map<int, obj*>'.[/color]
            >>
            >> But it *does* call delete if your map is of the form map<int,
            >> smart_ptr<T> > for some suitable definition of smart_ptr<T>
            >>
            >> I know: I'm using this all the time :-)[/color]
            >
            > But that's not the feature of the map. It's the destruction of the
            > 'smart_ptr<T>' object what does the 'delete'ion.[/color]

            Yep. That's the point of using something that can sort of "delegate"
            the destruction to the real thing, yet also pose as, the real thing
            when accessed from the map.
            --
            JFB

            Comment

            Working...