We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Cloning objects (Read 579 times)
Cloning objects
Apr 20th, 2010, 12:21am
 
What is the correct syntax to create an exact clone-object of any object? I've just realized that using the = sign to assign one object to the other seems to pass the reference to the identical object rather than copying it's values.
Re: Cloning objects
Reply #1 - Apr 20th, 2010, 1:11am
 
There is no correct syntax...
Cloning an object is complex, and highly depends on the application: if an object refers to other objects, should these objects be cloned as well or just their reference copied to the new object
There is a clone() method defined for all Java objects, but it precisely does a shallow copy, ie. all sub-objects are just reference to original's sub-objects. Beside, it is advised to avoid clone in favor of copy constructors and static factory methods (see the links in the given page).
Re: Cloning objects
Reply #2 - Apr 20th, 2010, 4:35am
 
Hmm. Thanks. I've never thought it through but I see the point now. So I have to make my code longer then :c)
Page Index Toggle Pages: 1