[英]What happens when a C++ anonymous object replaces previously assigned object?
This question concerns what happens to an object when a variable is assigned to a new object. 这个问题与将变量分配给新对象时对象发生什么情况有关。
Date {
public:
Date(int day, int month);
int day;
int month;
};
in main: 在主要方面:
Date birthday(7,4); //line 1
birthday = Date(5,5); //line 2
My understanding is that line 2's RS returns an anonymous object. 我的理解是第2行的RS返回一个匿名对象。 My question is, what happens to the object that was created with the values 7,4?
我的问题是,用值7,4创建的对象会怎样? Is it replaced in memory by the anonymous object?
它在内存中被匿名对象取代了吗?
You cannot replace some object with another. 您不能将某个对象替换为另一个。
What you can do, and what actually happens in your example, is move-assigning from a temporary object to some target-object. 您可以做的事,以及在示例中实际发生的事,是将分配从临时对象移动到某些目标对象。
If it was not a temporary object, move-assigning from it would not happen, instead copy-assigning would take place. 如果它不是临时对象,则不会发生从其进行移动分配,而是进行复制分配。
Neither changes which object it is, only what value it has. 两者都不会更改其对象,只会更改其具有的值。
BTW: Copy-assignment, Move-assignment, copy-construction, move-construction and destruction for your example-class are implicitly defined as member-by-member. 顺便说一句:示例类的复制分配,移动分配,复制构造,移动构造和销毁隐式定义为每个成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.