Java equivalent of C ++ copy assignment operator

I am trying to understand this operator function written in C ++ and convert it to Java.

Class& Class::operator=(const Class& In) { properties = In.properties; return *this; } 

Does it just copy the instance and properties of the class object? Why I already wrote something:

 public static Class copy(Class obj) { //returns new instance of Class individual Class copy = new Class(obj.row_num, obj.col_num, obj.input_length, obj.output_length, obj.max_arity, obj.function_length, obj.levels_back); copy.genes = obj.genes.clone(); return copy; } 

Am I on the right track? Many thanks for your help.

+7
java c ++ operator-keyword
source share
3 answers

Ampersand & denotes a link in C ++. You must ensure that the behavior is similar to what Java objects provide out of the box because Java manages objects through links.

In C ++, copying does not occur when sending help. In fact, avoiding copying is the main reason for using const references as function parameters.

The code you show also does not copy: it changes its state based on the value that is assigned. The closest way to simulate this in Java would be to provide an assign(Class other) method that changes the current state according to the current state of the object:

 Class assign(Class other) { this.properties = other.properties; return this; } 

You will need to use this method instead of assigning C ++, for example:

 Class clOne(args1); Class clTwo(args2); clOne = clTwo; // Using the assignment operator 

becomes the following:

 Class clOne = new Class(args1); Class clTwo = new Class(args2); clOne.assign(clTwo); // Using the assignment method instead of the operator 
+3
source share

You are pretty much on the right track. The copy assignment operator in C ++ is used for direct assignment (copying) from one object to another. Since Java objects are only accessible via links, such assignments do not make sense. To accurately combine C ++ semantics, the Java equivalent would be as follows:

 public Class copy(Class obj) { row_num = obj.row_num; col_num = obj.col_num; // etc., etc. genes = obj.genes.clone(); return this; } 
+2
source share

Am I on the right track?

View. But not really. C ++ distinguishes between reassigning an existing object and creating a new one.

Java does not work. You cannot reassign an existing object in Java 1 (but you can, of course, reassign a link). In Java, to copy an object (rather than assign a reference to it), you usually use the copy constructor:

 Class(Class other) { // Copy members of `other` into `this`. } 

And then use it like this:

 Class x = new Class(something here); Class y = new Class(x); // copy 

In particular, this is what all Java containers implement. I would not rely on clone . First of all, clone should be used only if the class implements the Cloneable tag Cloneable . Secondly, clone s design may be broken and its use is not recommended .


1 Well, you can, of course, reassign the members of the object (if they are not final ), and you could mimic the assignment operator of a C ++ copy by providing the assign to method. However, this is not an ordinary way of doing things in Java (although it may have its place in some exceptional cases).

+1
source share

All Articles