Item 10: Have Assignment Operators Return a Reference to *this

This is a convention that you should follow (unless you have great reason not to).

class Foo {
 public:
  Foo& operator=(const Foo& rhs) {
    // ...
    return *this;
  }

  // And +=, -=, etc.
  Foo& operator+=(const Foo& rhs) {
    // ...
    return *this;
  }

  // And assignment from other types, too.
  Foo& operator=(int rhs) {
    // ...
    return *this;
  }
}
Creative Commons License
This blog by Che-Liang Chiou is licensed under a Creative Commons Attribution 4.0 International License.