Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old April 21st, 2007, 01:15 AM
barcaroller
Guest
 
Posts: n/a
Default STL destruction question...

When a STL container is destroyed, will the destructor of the individual
objects be automatically called?





  #2  
Old April 21st, 2007, 01:25 AM
Ian Collins
Guest
 
Posts: n/a
Default Re: STL destruction question...

barcaroller wrote:
Quote:
When a STL container is destroyed, will the destructor of the individual
objects be automatically called?
>
Yes, assuming they have one!

--
Ian Collins.
  #3  
Old April 21st, 2007, 01:35 AM
barcaroller
Guest
 
Posts: n/a
Default Re: STL destruction question...


"Ian Collins" <ian-news@hotmail.comwrote in message
news:58t3hhF2i6eqtU23@mid.individual.net...
Quote:
barcaroller wrote:
Quote:
>When a STL container is destroyed, will the destructor of the individual
>objects be automatically called?
>>
Yes, assuming they have one!
Does this also apply to object pointers (e.g. set<Class*>)? In this case I
would create the object myself (using 'new Class') but I assume that, since
I created it, I should destroy it. However, there's no reason why the STL
container could not do that for me (by calling delete for each object).
Would it?





  #4  
Old April 21st, 2007, 01:35 AM
Mark P
Guest
 
Posts: n/a
Default Re: STL destruction question...

barcaroller wrote:
Quote:
"Ian Collins" <ian-news@hotmail.comwrote in message
news:58t3hhF2i6eqtU23@mid.individual.net...
Quote:
>barcaroller wrote:
Quote:
>>When a STL container is destroyed, will the destructor of the individual
>>objects be automatically called?
>>>
>Yes, assuming they have one!
>
Does this also apply to object pointers (e.g. set<Class*>)? In this case I
would create the object myself (using 'new Class') but I assume that, since
I created it, I should destroy it. However, there's no reason why the STL
container could not do that for me (by calling delete for each object).
Would it?
>
It will not and, if you consider it for a moment, you'll probably agree
that it should not.
  #5  
Old April 21st, 2007, 06:25 AM
Andre Kostur
Guest
 
Posts: n/a
Default Re: STL destruction question...

"barcaroller" <barcaroller@music.netwrote in
news:f0blum$j15$1@aioe.org:
Quote:
>
"Ian Collins" <ian-news@hotmail.comwrote in message
news:58t3hhF2i6eqtU23@mid.individual.net...
Quote:
>barcaroller wrote:
Quote:
>>When a STL container is destroyed, will the destructor of the
>>individual objects be automatically called?
>>>
>Yes, assuming they have one!
>
Does this also apply to object pointers (e.g. set<Class*>)? In this
case I would create the object myself (using 'new Class') but I assume
that, since I created it, I should destroy it. However, there's no
reason why the STL container could not do that for me (by calling
delete for each object). Would it?
Follow the logic rigidly. The destructors of the contained object are
called. The contained object is a Class*. The destructor for a Class* (or
any other pointer for that matter) is a no-op. You may wish to consider
using some sort of smart pointer class (but specifically not std::auto_ptr)
as the contained object. This way the contained object is a smart_ptr
<Class>, and thus ~smart_ptr<Class>() would be invoked. Or you could loop
over the container and delete each pointer by hand.
  #6  
Old April 21st, 2007, 10:15 AM
Gavin Deane
Guest
 
Posts: n/a
Default Re: STL destruction question...

On 21 Apr, 01:30, "barcaroller" <barcarol...@music.netwrote:
Quote:
"Ian Collins" <ian-n...@hotmail.comwrote in message
>
news:58t3hhF2i6eqtU23@mid.individual.net...
>
Quote:
barcaroller wrote:
Quote:
When a STL container is destroyed, will the destructor of the individual
objects be automatically called?
>
Quote:
Yes, assuming they have one!
>
Does this also apply to object pointers (e.g. set<Class*>)? In this case I
would create the object myself (using 'new Class')
That's one way of obtaining a Class*. Another way is to take the
address of an existing Class object - which may or may not have
dynamic storage duration (i.e. may or may not need delete'ing).
Quote:
but I assume that, since
I created it, I should destroy it.
Absolutely.
Quote:
However, there's no reason why the STL
container could not do that for me (by calling delete for each object).
STL containers could have been designed that way. But such behaviour
would rule out the possibility of storing pointers to anything other
than dynamically allocated memory. The code below would be broken -
something of a restriction on the way STL containers could be used.

#include <vector>

void foo()
{
int i = 42;
int* pi = &i;
std::vector<int*v;
v.push_back(pi);
}
Quote:
Would it?
Nope. The library designers opted not to impose the above restriction
on client code. You were right in your first assumption.

If you are storing pointers to dynamically allocated objects, you can
make your life easier by storing smart pointers that manage the delete
automatically. Boost has examples and I believe some are included in
std::tr1 if your compiler supports it.

Gavin Deane

  #7  
Old April 21st, 2007, 10:55 AM
James Kanze
Guest
 
Posts: n/a
Default Re: STL destruction question...

On Apr 21, 2:30 am, "barcaroller" <barcarol...@music.netwrote:
Quote:
"Ian Collins" <ian-n...@hotmail.comwrote in message
Quote:
news:58t3hhF2i6eqtU23@mid.individual.net...
Quote:
Quote:
barcaroller wrote:
Quote:
When a STL container is destroyed, will the destructor of the individual
objects be automatically called?
Quote:
Quote:
Yes, assuming they have one!
(Formally, all objects have destructors. Some destructors are
trivial, however.)
Quote:
Does this also apply to object pointers (e.g. set<Class*>)?
Of course. The pointer gets destroyed. (Of course, the
destructor of a pointer is trivial, so nothing much really
happens.)

The library does not persue pointers. Luckily, because in a lot
of cases, the set<Class*is used for navigation, and there are
still a lot of other pointers to the objects when the set gets
destructed.
Quote:
In this case I
would create the object myself (using 'new Class') but I assume that, since
I created it, I should destroy it.
Correct.
Quote:
However, there's no reason why the STL
container could not do that for me (by calling delete for each object).
Would it?
Only that it would break most common uses of sets of pointers.
It's really fairly rare for a container to have ownership of a
non-value object.

--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles