473,473 Members | 4,297 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Point two objects to the same location

Hi,

How can we make two objects point to the same location? For e.g. I have
an object obj1 of a class and I want to have another object obj2 to
point to the member variables of obj1. Something like:

TestClass ob1 = new TestClass();
...
...
...
TestClass ob2 = ob1; // This doesn't work; It just creates a copy of
ob1 in ob2 and both are independent.

ob2.changeMember(); // I want this change to be reflected in ob1

Any help will be appreciated.

Thanks,
Prateek

Jun 10 '06 #1
6 1787
si*************@gmail.com wrote:
Hi,

How can we make two objects point to the same location? For e.g. I have
an object obj1 of a class and I want to have another object obj2 to
point to the member variables of obj1. Something like:

TestClass ob1 = new TestClass();
..
..
..
TestClass ob2 = ob1; // This doesn't work; It just creates a copy of
ob1 in ob2 and both are independent.

ob2.changeMember(); // I want this change to be reflected in ob1

Any help will be appreciated.


Use a reference.

TestClass obj1 = new TestClass() ;
TestClass & obj2 = obj1 ;

--
Alan Johnson
Jun 10 '06 #2
si*************@gmail.com wrote:
Hi,

How can we make two objects point to the same location?
If you want to point, you might want to try a pointer. ;-)
An object _is_ a location (that contains data), so you can't have two
objects at the same location. C++ mandates that every object has a distinct
address.
For e.g. I have an object obj1 of a class and I want to have another
object obj2 to point to the member variables of obj1. Something like:

TestClass ob1 = new TestClass();
..
..
..
TestClass ob2 = ob1; // This doesn't work; It just creates a copy of
ob1 in ob2 and both are independent.
Try:

TestClass& ob2 = ob1;

That creates a reference to ob1. Now you can use ob2 and ob1 interchangably
to refer to the same object.
ob2.changeMember(); // I want this change to be reflected in ob1

Any help will be appreciated.


Jun 10 '06 #3
Try using pointers:

TestClass *ob1 = new TestClass ();
TestClass *ob2 = ob1;

Regards,

LuTHieR

Jun 10 '06 #4
Alan Johnson wrote:
How can we make two objects point to the same location?
Use a reference.

TestClass obj1 = new TestClass() ;
TestClass & obj2 = obj1 ;


Use a reference:

TestClass obj1;
TestClass & obj2 = obj1 ;

Don't use 'new' unless you absolutely need to, and don't use pointers unless
you need their features that references don't have. Until you learn what
these are, use references.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 10 '06 #5
Thanks all of you. I was stupid not to have figured this out.

Phlip wrote:
Alan Johnson wrote:
How can we make two objects point to the same location?

Use a reference.

TestClass obj1 = new TestClass() ;
TestClass & obj2 = obj1 ;


Use a reference:

TestClass obj1;
TestClass & obj2 = obj1 ;

Don't use 'new' unless you absolutely need to, and don't use pointers unless
you need their features that references don't have. Until you learn what
these are, use references.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


Jun 10 '06 #6

si*************@gmail.com wrote:
Thanks all of you. I was stupid not to have figured this out.


No, not stupid. We all start at the same place. The only stupid
question is the one never asked.

-Sandra

Jun 11 '06 #7

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

Similar topics

8
by: Piotre Ugrumov | last post by:
How can I implement a function that calculate the coordinates of a point, in 3 dimensional space? The function take 6 value (x, y, z, v(velocity), corner alfa, corner beta). The prototype is...
2
by: JR | last post by:
Hi. I have a CGI script that will need to call itself an unknown number of times, to add rows, run queries, etc. At the bottom of the output that is produced by the script, there are four...
1
by: Dirk Försterling | last post by:
Hi, sorry for reposting, but it seems my message just hit nothing but air. If I posted to the wrong list or did something else wrong with the message, please let me know. I really want to...
1
by: Bilo | last post by:
Need Help with System.Drawing.Point b = new System.Drawing.Point(int x , int y) I want to move a window when mous is down and mous is moving private void label1_MouseMove(object sender,...
0
by: Kevin | last post by:
Hi my name is Kevin I am currently working on a Asp 2.0 project and im having some ObjectDataSource Problems The thing is i have a Class called User and a Bussiness Logic Layer FcUser with...
7
by: thisismyidentity | last post by:
Hi all, I am trying to predict the behaviour of floating point load and store operations on integer locations. I ve written a small piece of code having some inline assembly code which I am...
1
by: Marc the Demi-Programmer | last post by:
I am overriding WncProc to make sure my form's location stays within specified parameters. Basically, it has to stay at the top of the screen and not be off to either of the sides. All that works...
0
by: NM | last post by:
Hello, I've got a problem inserting binary objects into the postgres database. I have binary objects (e.g. images or smth else) of any size which I want to insert into the database. Funny is it...
14
by: JoeC | last post by:
I have been writing games and I also read about good programming techniques. I tend to create large objects that do lots of things. A good example I have is a unit object. The object controls...
0
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,...
0
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...
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
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.