naavis
naavis

Reputation: 125

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? 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
    ...
}

Upvotes: 11

Views: 6832

Answers (3)

Jay Miller
Jay Miller

Reputation: 2234

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:

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.

Upvotes: 11

vsoftco
vsoftco

Reputation: 56547

Technically the operator= is invoked by the left hand side part of the assignement, such as

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 (i.e., variable in your case) is modified, and its individual members are copies of the members of the right hand side temporary.

Upvotes: 4

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

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. However, it is not these that you have asked about. Their value is copied into 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.

The temporary ClassName(c, d) goes out of scope at the end of the line in which you've used it; 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.

Upvotes: 4

Related Questions