| View previous topic :: View next topic |
| Author |
Message |
JustSomeGuy Guest
|
Posted: Tue Jun 29, 2004 5:28 pm Post subject: Passing refrences to objects... |
|
|
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
|
Posted: Tue Jun 29, 2004 5:44 pm Post subject: Re: Passing refrences to objects... |
|
|
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
|
Posted: Tue Jun 29, 2004 5:44 pm Post subject: Re: Passing refrences to objects... |
|
|
"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
|
Posted: Tue Jun 29, 2004 6:02 pm Post subject: Re: Passing refrences to objects... |
|
|
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
|
Posted: Tue Jun 29, 2004 7:42 pm Post subject: Re: Passing refrences to objects... |
|
|
"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
|
Posted: Tue Jun 29, 2004 7:47 pm Post subject: Re: Passing refrences to objects... |
|
|
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 |
|
 |
|