7

I'm 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 this simply copy instance and properties of a class object? For which I've already written 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 correct track? Many thanks for your help.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
p.drewello
  • 95
  • 1
  • 6

3 Answers3

3

Ampersand & designates a reference in C++. It is needed to provide the behavior similar to what Java objects provide "out of the box", because Java manages objects through references.

There is no copying going on in C++ when a reference is passed. In fact, avoiding copying is a major reason for using const references as function parameters.

The code that you show does not perform copying either: it changes its state based on the value being "assigned". The closest way of modeling this in Java would be providing an assign(Class other) method that changes the current state to match that of the object passed in:

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

You will need to use this method in place of C++'s assignment, like this:

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

becomes this:

Class clOne = new Class(args1);
Class clTwo = new Class(args2);
clOne.assign(clTwo); // Using the assignment method instead of the operator
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

Am I on the correct track?

Kind of. But not quite. C++ distinguishes between reassigning an existing object and creating a new one.

Java doesn’t. You cannot reassign to an existing object in Java1 (but you can of course reassign a reference). In Java, in order to copy an object (rather than assign a reference to it), you would usually use a copying constructor:

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

And then use it as follows:

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

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


1 Well you could of course reassign the members of an object (unless they are final), and you could mimic C++’s copy assignment operator by providing a method assign to do that. However, this isn’t the conventional way of doing things in Java (although it might have its place in some exceptional instances).

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
2

You're pretty much on the right track. The copy assignment operator in C++ is used when directly assigning (copying) from one object to another. As Java objects are only accessible via references, such assignments are meaningless. To match the C++ semantics exactly, the Java equivalent would be:

public Class copy(Class obj) {
    row_num = obj.row_num;
    col_num = obj.col_num;
    // etc., etc.
    genes = obj.genes.clone();
    return this;
}
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455