map::erase

V

Viral Shah

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.
 
V

Victor Bazarov

Viral said:
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
 
J

John Dibling

Viral said:
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
 
V

verec

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

Victor Bazarov

verec said:
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
 
V

verec

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top