Java相当于C ++拷贝赋值运算符

时间:2022-02-28 21:41:42

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

我试图理解用C ++编写的这个操作符函数并将其转换为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.

我在正确的轨道上吗?非常感谢您的帮助。

3 个解决方案

#1


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.

Ampersand&用C ++指定引用。需要提供类似于Java对象提供的“开箱即用”的行为,因为Java通过引用来管理对象。

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.

传递引用时,C ++中没有复制。实际上,避免复制是使用const引用作为函数参数的主要原因。

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:

您显示的代码也不执行复制:它根据“已分配”的值更改其状态。在Java中建模的最接近的方法是提供一个assign(Class other)方法,该方法改变当前状态以匹配传入的对象的状态:

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:

您将需要使用此方法代替C ++的赋值,如下所示:

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

#2


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:

你几乎走在正确的轨道上。当直接从一个对象分配(复制)到另一个对象时,使用C ++中的复制赋值运算符。由于Java对象只能通过引用访问,因此这些分配毫无意义。为了完全匹配C ++语义,Java等价物将是:

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

#3


2  

Am I on the correct track?

我在正确的轨道上吗?

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

有点。但并不完全。 C ++区分重新分配现有对象和创建新对象。

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:

Java没有。您无法重新分配给Java1中的现有对象(但您当然可以重新分配参考)。在Java中,为了复制对象(而不是为其分配引用),通常使用复制构造函数:

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.

特别是,这是所有Java容器实现的。我不会依赖克隆。首先,只有在类实现标记接口Cloneable时才应使用clone。其次,克隆的设计可以说是破坏了,不建议使用它。


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).

1当然,您可以重新分配对象的成员(除非它们是最终的),并且您可以通过提供方法分配来模仿C ++的复制赋值运算符。但是,这不是传统的Java处理方式(尽管它可能在某些特殊情况下有它的位置)。

#1


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.

Ampersand&用C ++指定引用。需要提供类似于Java对象提供的“开箱即用”的行为,因为Java通过引用来管理对象。

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.

传递引用时,C ++中没有复制。实际上,避免复制是使用const引用作为函数参数的主要原因。

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:

您显示的代码也不执行复制:它根据“已分配”的值更改其状态。在Java中建模的最接近的方法是提供一个assign(Class other)方法,该方法改变当前状态以匹配传入的对象的状态:

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:

您将需要使用此方法代替C ++的赋值,如下所示:

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

#2


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:

你几乎走在正确的轨道上。当直接从一个对象分配(复制)到另一个对象时,使用C ++中的复制赋值运算符。由于Java对象只能通过引用访问,因此这些分配毫无意义。为了完全匹配C ++语义,Java等价物将是:

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

#3


2  

Am I on the correct track?

我在正确的轨道上吗?

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

有点。但并不完全。 C ++区分重新分配现有对象和创建新对象。

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:

Java没有。您无法重新分配给Java1中的现有对象(但您当然可以重新分配参考)。在Java中,为了复制对象(而不是为其分配引用),通常使用复制构造函数:

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.

特别是,这是所有Java容器实现的。我不会依赖克隆。首先,只有在类实现标记接口Cloneable时才应使用clone。其次,克隆的设计可以说是破坏了,不建议使用它。


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).

1当然,您可以重新分配对象的成员(除非它们是最终的),并且您可以通过提供方法分配来模仿C ++的复制赋值运算符。但是,这不是传统的Java处理方式(尽管它可能在某些特殊情况下有它的位置)。