Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Passing refrences to objects...

Reply
Thread Tools

Passing refrences to objects...

 
 
JustSomeGuy
Guest
Posts: n/a
 
      06-29-2004
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



 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      06-29-2004
JustSomeGuy wrote:

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

 
Reply With Quote
 
 
 
 
Chris Theis
Guest
Posts: n/a
 
      06-29-2004

"JustSomeGuy" <> wrote in message
news:...
> 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


 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      06-29-2004
On Tue, 29 Jun 2004 11:28:09 -0600, JustSomeGuy <>
wrote:

>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

 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      06-29-2004

"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
news:...
[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.

Cheers
Chris


 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      06-29-2004
On Tue, 29 Jun 2004 21:42:34 +0200, "Chris Theis"
<> wrote:

>
>"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
>news:.. .
>[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

 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BUG: debugger is case-sensitive to paths when resolving assembly refrences PJ6 ASP .Net 0 09-21-2006 04:40 PM
Dynamically Calling Web Refrences Josh ASP .Net 0 08-09-2004 02:45 AM
JSP-Java, object refrences Colin Northway Java 1 02-18-2004 10:44 AM
newbie question on refrences. JustSomeGuy Java 5 09-10-2003 04:10 PM
trouble adding project refrences sonic_soul ASP .Net 0 08-28-2003 02:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57