• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cloning and assigning??

 
Vijay Kashyap
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could any body tell me whats the difference between assigning an object to another and cloning it?


Why would I require to clone any object , cannot I assign it??
Regards,
Vijay
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You always assign references to objects. In your example, obj and obj1 are referencing the same object, that is, if you call a method on obj1 it is the same as calling a method on obj. Therefore, if you want to work on a copy of an object, you have to clone it like you did for obj2.
 
Vijay Kashyap
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ilja,
By this you mean that the cloned object obj2 will not share the same reference as obj but will create a new reference.
Am I right?
Thanks,
Vijay
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not exactly. By this he means that the cloned object obj2 will not reference the same object as obj - a new object has been created.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic