简体   繁体   English

在C ++中重新分配对象变量时,原始对象会发生什么?

[英]When object variable is reassigned in C++, what happens to the original object?

When an object variable is reassigned in C++, what happens to the original value? 在C ++中重新分配对象变量时,原始值会发生什么变化? In the code below an object is created onto the stack and placed in variable . 在下面的代码中,对象被创建到堆栈上并放在变量中 Then a new object is created on the stack and placed in the same variable. 然后在堆栈上创建一个新对象并将其放在同一个变量中。 What happens to the original object? 原始对象会发生什么? Does it stay on the stack until variable goes out of scope? 变量超出范围之前,它是否会保留在堆栈中?

void foo() {
    ClassName variable(a, b); // variable created on the stack
    variable = ClassName(c, d); // new value for variable created on stack
    ...
}

What happens is that the class's assignment operator is called. 会发生什么是调用类的赋值运算符。 In most cases that just means that the contents of the old object are updated with values from the new object. 在大多数情况下,这意味着旧对象的内容使用新对象的值进行更新。 So if ClassName is: 所以如果ClassName是:

struct ClassName
{
    int a;
    int b;

    ClassName(int a, int b) : a(a), b(b) {}
};

In this case the default assignment operator would be called which would be equivalent to: 在这种情况下,将调用默认赋值运算符,它等效于:

    ClassName& operator=(const ClassName& other)
    {
        a = other.a;
        b = other.b;
        return *this;
    }

For classes that have dynamic content there would be a bit more to do, but the results will generally be the same. 对于具有动态内容的类,可能会有更多事情要做,但结果通常是相同的。 Since the assignment operator can be overridden, anything could theoretically happen but this is what we expect. 由于可以覆盖赋值运算符,理论上可以发生任何事情,但这正是我们所期望的。

Actually, nothing happens to the object: you're still using it ! 实际上,对象没有任何反应: 你还在使用它

Assignment doesn't replace the entire object with a different object: it invokes that original object's assignment operator, allowing the object to make it look like something new, even though it's not. 赋值不会用不同的对象替换整个对象:它调用原始对象的赋值运算符,允许对象使其看起来像新的东西,即使它不是。

For example: 例如:

int x = 1;
x = 2;

You have only declared one object here, even though its value changes. 您只在这里声明了一个对象,即使它的值发生了变化。

Of course there are indeed multiple objects in play in even this simple snippet — both the 1 and the 2 are integer literals and temporary objects. 当然,即使是这个简单的片段,确实有多个对象正在发挥作用 - 12都是整数文字和临时对象。 However, it is not these that you have asked about. 但是,您问的不是这些。 Their value is copied into x . 它们的值被复制到x

Similarly, in your own code you copy the "value" of the temporary ClassName(c, d) into variable , but variable is still the original variable . 同样,在您自己的代码中,您将临时ClassName(c, d)的“值”复制到variable ,但variable仍然是原始variable

The temporary ClassName(c, d) goes out of scope at the end of the line in which you've used it; 临时ClassName(c, d)超出了您使用它的行末尾的范围; the bytes used to represent it (unless optimised out) will probably reside within the stack frame until you leave the function scope, though you will not legally be able to read them. 用于表示它的字节(除非被优化掉)可能会驻留在堆栈帧中,直到您离开函数范围,尽管您在法律上无法读取它们。

Technically the operator= is invoked by the left hand side part of the assignement, such as 从技术上讲, operator=由分配的左手侧部分调用,例如

variable.operator=(ClassName(c,d));

In your case, if you don't explicitly define the assignment operator, the compiler generates a default one for you, which copies the right hand side using the copy assignment operators of its individual members. 在您的情况下,如果您没有明确定义赋值运算符,编译器会为您生成一个默认值,它使用其各个成员的副本赋值运算符复制右侧。 So the left hand side (ie, variable in your case) is modified, and its individual members are copies of the members of the right hand side temporary. 所以左侧(即您的情况中的variable )被修改,其各个成员是右侧临时成员的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c ++中的对象调用不带参数的成员函数时会发生什么 - What happens when a member function with no arguments is called by an object in c++ 通过赋值初始化C ++对象时会发生什么? - What happens when you initialize a C++ object by assignment? 在C ++中为对象分配引用的值时会发生什么? - What happens when I assign an object the value of a reference in C++? ARC和C ++对象中的Objective C对象会发生什么? - What happens with ARC and an Objective C object inside a C++ object? 当C ++匿名对象替换先前分配的对象时会发生什么? - What happens when a C++ anonymous object replaces previously assigned object? 当我们在C ++中重新初始化对象时会发生什么,我非常困惑 - very confused about what happens when we reinitialize the object in c++ C ++中的函数const本地对象会发生什么? - What happens to the function const local object in C++? 当我通过引用传递一个对象并且它超出范围时,C ++会发生什么? - What happens in C++ when I pass an object by reference and it goes out of scope? c ++在一个线程中写入并在第二个线程中读取同一对象时会发生什么? (安全吗?) - c++ what happens when in one thread write and in second read the same object? (is it safe?) 与 C++ 中未命名的 object 交换后旧的会发生什么 - What happens to the old one after swapping with an unnamed object in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM