473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

Jul 22 '05 #1
5 1522
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.

Jul 22 '05 #2

"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:40***************@ucalgary.ca...
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
Jul 22 '05 #3
On Tue, 29 Jun 2004 11:28:09 -0600, JustSomeGuy <No***@ucalgary.ca>
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
No**********@Home.com
Jul 22 '05 #4

"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
news:5i********************************@4ax.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.

Cheers
Chris
Jul 22 '05 #5
On Tue, 29 Jun 2004 21:42:34 +0200, "Chris Theis"
<Ch*************@nospam.cern.ch> wrote:

"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
news:5i********************************@4ax.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
No**********@Home.com
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
1
by: robertm | last post by:
I have the following method written in C# public static void Substitute(ControlCollection controls, ArrayList affectedControls, ref CCase objCase) { // .................. }
6
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A...
0
by: Peter | last post by:
I'm trying to figure out how to send records from my asp webpage recordset into a .dll, which then exports the data to an excel document on the server. My Data consists of two fields The...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
3
by: Afshar | last post by:
Hi, I am writing a single .cs in IDE that uses some refrences that I have created previously as dll. I compile this refrences with /r switch in csc.exe with no problem. IntelliSense works with...
9
by: Greger | last post by:
Hi, I am building an architecture that passes my custom objects to and from webservices. (Our internal architecture requires me to use webservices to any suggestion to use other remoting...
28
by: SpreadTooThin | last post by:
I'm really worried that python may is doing some things I wasn't expecting... but lets see... if I pass a list to a function def fn(myList): and in that function I modify an element in the...
4
by: Deckarep | last post by:
Hello fellow C# programmers, This question is more about general practice and convention so here goes: I got into a discussion with a co-worker who insisted that as a general practice all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.