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.
IndexProgramming Questions & HelpSyntax Questions › how to force a deep copy
Page Index Toggle Pages: 1
how to force a deep copy? (Read 1117 times)
how to force a deep copy?
May 16th, 2010, 8:29pm
 
I'm trying to copy a value from a class attribute to an integer variable, but when I change that variable, it also changes the class attribute.

int I = (int) currentConf.iIndex;
int j = (int) currentConf.jIndex;

moreover I want to make a deep copy of an object to fit it into an array. but when I modify the object in the array the original object gets modified also.

please help, cause I didn't find any help in the tutorial or in the reference.
Re: how to force a deep copy?
Reply #1 - May 16th, 2010, 10:16pm
 
the Java Cloneable interface might be what you're looking for.  I think you just add "implements Cloneable" to your class declaration, and use object.clone() to get your copy.

Fast/dirty: I usually just make a new object (object n = new object( oldObj.var1, oldObj.var2, etc) with the old object's variables.
Re: how to force a deep copy?
Reply #2 - May 17th, 2010, 12:59am
 
Quote:
when I change that variable, it also changes the class attribute

Surprising, it shouldn't do that, not with the code you show.
Quote:
I want to make a deep copy of an object to fit it into an array.

I don't understand what you try to do.
Showing a significant code fragment might help.

Quote:
I think you just add "implements Cloneable" to your class declaration, and use object.clone() to get your copy.

Half true... Smiley It will copy only primitive types, IIRC, and perhaps objects, but actually only their references: ie. if an object holds a reference to another, the cloned one will reference the same object.
The solution is to override clone(), or, better:
Quote:
I usually just make a new object (object n = new object( oldObj.var1, oldObj.var2, etc) with the old object's variables.

That's not dirty, it is a recommended practice. Or, a slightly cleaner way is to use a copy constructor: object n = new object(oldObject);
That's less parameters to pass around... Smiley
Re: how to force a deep copy?
Reply #3 - May 17th, 2010, 1:25pm
 
ok, let's see if I can be clearer: I have a class (I really wanted a struct but I don't think processing has something like that) defined as:
Code:
class quickSortConfig {
 int iIndex;
 int jIndex;
 int pivotIndex;
 int confStatus;
 int leftWall;
 int rightWall;
 
 quickSortConfig()
 {
   iIndex = -1;
   jIndex = -1;
   pivotIndex = -1;
   confStatus = -1;
   leftWall = -1;
   rightWall = -1;
 }
 quickSortConfig( quickSortConfig that )
 {
   this.iIndex = that.iIndex;
   this.jIndex = that.jIndex;
   this.pivotIndex = that.pivotIndex;
   this.confStatus = that.confStatus;
   this.leftWall = that.leftWall;
   this.rightWall = that.rightWall;
   
 }
}


then I want 2 of those objects called small and large with the attributes taken in part from another ojbect:
Code:
 quickSortConfig small = currentConf;
 small.leftWall = currentConf.leftWall;
 small.rightWall = I - 1 ;
 small.iIndex = small.leftWall;
 small.jIndex = small.rightWall - 1;
 small.pivotIndex = (small.rightWall - small.leftWall) / 2;
 
 println("small " + small.pivotIndex + " " +
   small.iIndex + " " + small.jIndex + " " +
   small.leftWall + " " + small.rightWall
    + " " + nextConfig.rightWall
   );
   
 quickSortConfig large = currentConf;
 large.leftWall = I + 1 ;
 large.iIndex = large.leftWall;
 large.jIndex = large.rightWall - 1;
 large.pivotIndex = (large.rightWall - large.leftWall) / 2;


the problem here is that the currentConf object is modified when I modify the small object and thus when I initialize large to be currentConf it is actually = to small
Re: how to force a deep copy?
Reply #4 - May 17th, 2010, 2:18pm
 
A class is actually a struct, you can use it as such if you don't add constructors and methods... Smiley
Quote:
quickSortConfig small = currentConf;

You don't do a copy there. currentConf holds a reference to an object, and this assignment actually set in small the same reference to that object.
Your class has a copy constructor we talk about, just use it:
quickSortConfig small = new quickSortConfig(currentConf);
Page Index Toggle Pages: 1