470,810 Members | 901 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

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 1668
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Piotre Ugrumov | last post: by
7 posts views Thread by thisismyidentity | last post: by
14 posts views Thread by JoeC | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.