473,772 Members | 3,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

"JustSomeGu y" <No***@ucalgary .ca> wrote in message
news:40******** *******@ucalgar y.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**********@Ho me.com
Jul 22 '05 #4

"Bob Hairgrove" <wouldnt_you_li ke@to_know.com> wrote in message
news:5i******** *************** *********@4ax.c om...
[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.c h> wrote:

"Bob Hairgrove" <wouldnt_you_li ke@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**********@Ho me.com
Jul 22 '05 #6

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

Similar topics

3
14950
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
1
1560
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
3255
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 query-string flag is used to indicate to the page whether it should instantiate a new instance of the object or access an existing instance from the calling page. On the both pages I have a property of the page which is an instance of this custom...
0
809
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 server does not have excel installed on it so I want to create a project that will house all the needed refrences. I'll be the only user using this tool. Help. How do I create a vb.net dll to do this?
11
8131
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 number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
3
1569
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 cs commands and syntaxes. But the problem is It doesn't know my own refrences and so doesn't show my class members. What can I do with with these? Please Help! Regards,
9
3803
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 techniques are not feasible) The question is; Given that I have a Person object with a private set for id. What is the recommended approac in passing that object to the web service
28
1231
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 list, then does the callers list get modied as well. def fn(list): list = 0
4
2814
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 objects should be passed by reference using the ref keyword generally speaking because as the writer of code you are conveying your intentions that an Object should/can be modified by your function.
0
9620
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9912
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.