C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Passing refrences to objects...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
JustSomeGuy
Guest





PostPosted: Tue Jun 29, 2004 5:28 pm    Post subject: Passing refrences to objects... Reply with quote



Silly question maybe...
I pass a refrence to an object to a method...
The method adds the object to a (stl) list of these objects...
However if a refrence to the object is passed and then added list...
if the original object is destroyed.. is the object in the list invalid
or is a copy of the object
made by the stl list for insertion purposes



Back to top
Rolf Magnus
Guest





PostPosted: Tue Jun 29, 2004 5:44 pm    Post subject: Re: Passing refrences to objects... Reply with quote



JustSomeGuy wrote:

Quote:
Silly question maybe...
I pass a refrence to an object to a method...
The method adds the object to a (stl) list of these objects...
However if a refrence to the object is passed and then added list...
if the original object is destroyed.. is the object in the list
invalid or is a copy of the object
made by the stl list for insertion purposes

The list contains a copy of the object, so destroying the original won't
affect the list.


Back to top
Chris Theis
Guest





PostPosted: Tue Jun 29, 2004 5:44 pm    Post subject: Re: Passing refrences to objects... Reply with quote




"JustSomeGuy" <NoOne (AT) ucalgary (DOT) ca> wrote

Quote:
Silly question maybe...
I pass a refrence to an object to a method...
The method adds the object to a (stl) list of these objects...
However if a refrence to the object is passed and then added list...
if the original object is destroyed.. is the object in the list invalid
or is a copy of the object
made by the stl list for insertion purposes


In the container classes of the standard library copies of the passed
objects are inserted. But you can easily try & check this yourself.

HTH
Chris



Back to top
Bob Hairgrove
Guest





PostPosted: Tue Jun 29, 2004 6:02 pm    Post subject: Re: Passing refrences to objects... Reply with quote

On Tue, 29 Jun 2004 11:28:09 -0600, JustSomeGuy <NoOne (AT) ucalgary (DOT) ca>
wrote:

Quote:
Silly question maybe...
I pass a refrence to an object to a method...
The method adds the object to a (stl) list of these objects...
However if a refrence to the object is passed and then added list...
if the original object is destroyed.. is the object in the list invalid
or is a copy of the object
made by the stl list for insertion purposes

You cannot create containers of references, only of objects or
pointers.

If you allocate memory for an object with "new" and store a pointer to
that object in an STL container, you need to ensure that you call
"delete" on every pointer before the container is cleared.

--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]

Back to top
Chris Theis
Guest





PostPosted: Tue Jun 29, 2004 7:42 pm    Post subject: Re: Passing refrences to objects... Reply with quote


"Bob Hairgrove" <wouldnt_you_like (AT) to_know (DOT) com> wrote

[SNIP]
Quote:

If you allocate memory for an object with "new" and store a pointer to
that object in an STL container, you need to ensure that you call
"delete" on every pointer before the container is cleared.


This is not necessarily true. The standard library containers also create
copies of pointers and it depends very much what you want to achieve whether
a delete statement should/must be issued before the container is cleared.
Imagine for example a container of pointers to geometric primitives and
another container where you store the pointers to the subset of the
primitives that have been selected. If you called a delete statement on the
pointers in the selection container then youŽd send all those primitives
into nirvana which is certainly not what you normally would want.

Cheers
Chris



Back to top
Bob Hairgrove
Guest





PostPosted: Tue Jun 29, 2004 7:47 pm    Post subject: Re: Passing refrences to objects... Reply with quote

On Tue, 29 Jun 2004 21:42:34 +0200, "Chris Theis"
<Christian.Theis (AT) nospam (DOT) cern.ch> wrote:

Quote:

"Bob Hairgrove" <wouldnt_you_like (AT) to_know (DOT) com> wrote in message
news:5ib3e0976l2jmerpusv2co1jq6ausm5ttn (AT) 4ax (DOT) com...
[SNIP]

If you allocate memory for an object with "new" and store a pointer to
that object in an STL container, you need to ensure that you call
"delete" on every pointer before the container is cleared.


This is not necessarily true. The standard library containers also create
copies of pointers and it depends very much what you want to achieve whether
a delete statement should/must be issued before the container is cleared.
Imagine for example a container of pointers to geometric primitives and
another container where you store the pointers to the subset of the
primitives that have been selected. If you called a delete statement on the
pointers in the selection container then youŽd send all those primitives
into nirvana which is certainly not what you normally would want.

You are quite correct ... of course, it is a question of who "owns"
the memory allocated for the pointers. In most cases I have seen, it
will be whatever is controlling the container, though (or perhaps some
other container higher up in a certain application-defined hierarchy,
as you suggest). The OP's question seemed to indicate that it was an
issue to be pointed out.

--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.