Skip to main content

Item 10: Have assignment operators return a reference to *this

Concept

The convention for assignment operators (including =, +=, -=, *=, etc.) is to return a reference to *this. This enables chaining of assignments like x = y = z = 15. All built-in types follow this convention, and so do all types in the standard library. While the convention is not enforced by the compiler, following it ensures your types work seamlessly with idiomatic C++ code.

Code Example

class Widget {
public:
// Standard copy assignment
Widget& operator=(const Widget& rhs) {
// ... assignment logic ...
return *this; // return reference to left-hand object
}

// Also applies to +=, -=, *=, etc.
Widget& operator+=(const Widget& rhs) {
// ... addition logic ...
return *this;
}

// Even applies to unconventional parameter types
Widget& operator=(int rhs) {
// ... assignment logic ...
return *this;
}
};

// Now chaining works as expected:
Widget w1, w2, w3;
w1 = w2 = w3; // equivalent to w1 = (w2 = w3)

Full source code

Things to Remember

  • Have assignment operators return a reference to *this.
  • This applies to all assignment operators, not just the standard form (operator=).
  • Item 11 — Handle assignment to self in operator=
  • Item 12 — Copy all parts of an object