C++ assignment operator: return a reference with *this

Understand why C++ operator= returns Point&, how to check the result's identity, and where chaining, self-assignment and auto copies can surprise you.

C++By
Published on

You're implementing a class and encounter Point& operator=(const Point& other). Why the two references, and why end with return *this when the members have already been copied? The signature answers two separate questions: how to read the source, and which object the assignment expression should refer to.

In short

The usual copy assignment operator has the form T& operator=(const T& other): it copies the value of other into the current object, then returns a reference to that object with return *this. The result of a = b therefore refers to a, and a = b = c groups as a = (b = c). Returning a reference follows the behavior of built-in assignment; a hand-written overload can return a different type, with different consequences.

What do the two references in the signature mean?

const Point& other lets the function read its source without copying it to pass the argument, and without modifying it through that reference. The Point& before the function name describes the result: a mutable reference to the destination.

point.hpp
#ifndef POINT_HPP#define POINT_HPP
struct Point {    int x;    int y;
    Point(int a = 0, int b = 0) : x(a), y(b) {}    Point(const Point& other) : x(other.x), y(other.y) {}
    Point& operator=(const Point& other) {        x = other.x;        y = other.y;        return *this;    }};
#endif

In a = b, the function is called on a, and other refers to b. this is a pointer to the current object; *this is an expression referring to that object. The return type Point& makes the function return a reference. The star alone doesn't mean "return by reference": with a Point return type, the same statement creates a result by value.

return this would provide a pointer, which doesn't match the declared Point& result. Returning other is unsuitable too: its reference is const, and the expected result refers to the destination. If pointers still feel unclear, revisit the distinction between an address and the pointed-to object.

How can you check that a = b returns a?

Bind the result to a reference, then compare addresses. Equal values aren't enough: a separate copy could hold the same coordinates.

reference.cpp
#include "point.hpp"#include <iostream>
int main() {    Point a(1, 2), b(3, 4), c(5, 6);    Point& result = (a = b);    std::cout << std::boolalpha << (&result == &a) << '\n';    result.x = 9;    std::cout << a.x << ' ' << b.x << '\n';    a = b = c;    std::cout << a.x << ' ' << b.x << ' ' << c.x << '\n';    (a = b) = Point(7, 8);    std::cout << a.x << ' ' << b.x << '\n';    a = a;    std::cout << a.x << ' ' << a.y << '\n';}
Terminal
$ g++ -std=c++98 -Wall -Wextra -Werror reference.cpp -o reference && ./referencetrue9 35 5 57 57 8

The first line confirms that result and a refer to the same object. Writing to result.x therefore changes a.x. b.x stays at 3: assignment copied the coordinates; it didn't turn a into an alias for b.

Chained assignment first assigns c to b, then uses the reference to b to assign a. All three objects still exist separately after those two calls.

The two assignments in a = b = c

Step 1 of 3

Before

expression
a = (b = c)
  1. a{9, 4}
  2. b{3, 4}
  3. c{5, 6}

Each object has its own coordinates.

Read all steps
  1. 1. Before

    expression
    a = (b = c)
    1. a{9, 4}
    2. b{3, 4}
    3. c{5, 6}

    Each object has its own coordinates.

  2. 2. After b = c

    result
    reference to b
    1. a{9, 4}
    2. b{5, 6}result
    3. c{5, 6}

    This call's result refers to b.

  3. 3. After a = b

    result
    reference to a
    1. a{5, 6}result
    2. b{5, 6}
    3. c{5, 6}

    The complete expression's result refers to a.

The line (a = b) = Point(7, 8) checks another detail: the second assignment acts on a. This is mainly a test of the return contract, rather than a reason to make everyday expressions harder to read.

Does returning by value prevent all chaining?

No. With a const T& parameter, a = b = c can still compile when the operator returns T by value. The intermediate result is then a copy. Here's a deliberately different implementation:

value.cpp
#include <iostream>
struct ValuePoint {    int x;    ValuePoint(int value = 0) : x(value) {}    ValuePoint(const ValuePoint& other) : x(other.x) {}    ValuePoint operator=(const ValuePoint& other) {        x = other.x;        return *this;    }};
int main() {    ValuePoint a(1), b(2), c(3);    a = b = c;    std::cout << a.x << ' ' << b.x << ' ' << c.x << '\n';    (a = b) = ValuePoint(9);    std::cout << a.x << ' ' << b.x << '\n';}
Terminal
$ g++ -std=c++98 -Wall -Wextra -Werror value.cpp -o value && ./value3 3 33 3

The simple chain does copy 3 into all three objects. But (a = b) = ValuePoint(9) assigns 9 to a temporary returned by value: a stays at 3. With the reference return in the first program, that second assignment changed a.

An overload returning void allows a standalone assignment but supplies no value for the next assignment. Returning const T& allows some chains but prevents modifying the destination through the result. The usual T& result preserves expected behavior without an extra return copy.

Why does auto still make a copy?

auto copy = (a = b) deduces a Point object here, rather than a reference. In C++11 and later, use auto& to bind a mutable reference to the result.

auto.cpp
#include "point.hpp"#include <iostream>
int main() {    Point a(1, 2), b(3, 4);    auto copy = (a = b);    auto& alias = (a = b);    copy.x = 8;    alias.y = 9;    std::cout << a.x << ' ' << a.y << '\n';    std::cout << copy.x << ' ' << copy.y << '\n';    std::cout << std::boolalpha << (&alias == &a) << '\n';}
Terminal
$ g++ -std=c++11 -Wall -Wextra -Werror auto.cpp -o auto && ./auto3 98 4true

Writes through copy and alias reach different objects. Returning a reference doesn't force the caller to keep that reference.

Similarly, Point copy = a constructs a new object from a: that's copy initialization. With two objects already constructed, copy = a calls the assignment operator. The = sign alone doesn't identify the operation; check whether it's part of a declaration.

Do you need to test this != &other?

Not for this two-integer class. a = a assigns each integer to itself and preserves the value, as the final line of the first program shows. The requirement is that self-assignment works; an address check is only one possible technique.

If a class owns an allocated resource, releasing the destination before reading the source can destroy the source itself during self-assignment. Simply adding return *this doesn't settle resource ownership or exception safety. Types that already manage their resources can often provide memberwise copying without a custom assignment operator. Understanding stack and heap helps locate objects, but doesn't by itself determine their copy semantics.

For a type containing only these two integers, implicit assignment is sufficient. Since C++11, Point& operator=(const Point&) = default; can also explicitly request compiler generation. The manual implementation above is useful for understanding the contract and for exercises that require writing the operator.

C++

coord_pair

32-bit

Implement assignment and copy construction for a pair of coordinates in C++98, then the remaining operators required by the task. Separately check that your assignment's returned reference refers to its destination.

Open the exercise

Frequently asked questions

Does the language require T& as the return type?

Not for a hand-written assignment overload. It's the conventional form that follows built-in assignment behavior. An implicitly declared copy assignment operator returns T&.

Can return *this leave a dangling reference?

It refers to the object the method was called on, rather than a local variable created inside the method. The reference remains usable while that object exists; returning a reference doesn't extend its lifetime.

Does the code work in C++98?

The first two programs do. The auto example and the = default syntax require C++11 or later.

Sources

Now write it yourself.