map::erase

Discussion in 'C++' started by Viral Shah, Jun 21, 2005.

  1. Viral Shah

    Viral Shah Guest

    I have a map<int, obj>. When I do a map::erase(iterator), does this call the
    destructor of the object? Or does it just remove that entry from the map.
     
    Viral Shah, Jun 21, 2005
    #1
    1. Advertising

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


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

    V
     
    Victor Bazarov, Jun 21, 2005
    #2
    1. Advertising

  3. Viral Shah

    John Dibling Guest

    Viral Shah wrote:
    > I have a map<int, obj>. When I do a map::erase(iterator), 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*>'.

    Take care,

    John Dibling
     
    John Dibling, Jun 21, 2005
    #3
  4. Viral Shah

    verec Guest

    On 2005-06-21 21:46:53 +0100, "John Dibling" <> said:

    > Viral Shah wrote:
    >> I have a map<int, obj>. When I do a map::erase(iterator), 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*>'.


    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
     
    verec, Jun 22, 2005
    #4
  5. verec wrote:
    > On 2005-06-21 21:46:53 +0100, "John Dibling" <>
    > said:
    >> Viral Shah wrote:
    >>> I have a map<int, obj>. When I do a map::erase(iterator), 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*>'.

    >
    > 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 :)


    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
     
    Victor Bazarov, Jun 22, 2005
    #5
  6. Viral Shah

    verec Guest

    On 2005-06-22 02:36:42 +0100, "Victor Bazarov" <> said:

    > verec wrote:
    >> On 2005-06-21 21:46:53 +0100, "John Dibling" <>
    >> said:
    >>> Viral Shah wrote:
    >>>> I have a map<int, obj>. When I do a map::erase(iterator), 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*>'.

    >>
    >> 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 :)

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


    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
     
    verec, Jun 22, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Angus Leeming
    Replies:
    5
    Views:
    2,945
    Howard Hinnant
    Feb 4, 2004
  2. Pieter Thysebaert

    STL std::map erase

    Pieter Thysebaert, May 13, 2004, in forum: C++
    Replies:
    26
    Views:
    14,309
    Robbie Hatley
    May 14, 2004
  3. jose luis fernandez diaz

    erase elements while iterating on a map

    jose luis fernandez diaz, May 28, 2004, in forum: C++
    Replies:
    3
    Views:
    11,593
    Derek
    May 28, 2004
  4. Gernot Frisch

    std::map - erase+continue

    Gernot Frisch, Mar 7, 2005, in forum: C++
    Replies:
    17
    Views:
    2,934
    Andre Kostur
    Mar 9, 2005
  5. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    381
    Pete Becker
    Mar 30, 2006
Loading...

Share This Page