Item 11: Handle Assignment to Self in operator=

Self-assignment could happen when objects are aliased (e.g., *px = *py).

Three solutions:

  • (Traditional) Test identity: this == &rhs.

  • Exception-safe copy, which happens to be also self-assignment-safe, too (although not the most efficient self-assignment solution).

Foo& Foo::operator=(const Foo& rhs) {
  Bar *orig = bar;
  bar = new Bar(*rhs.bar);  // Make a copy.
  delete bar;
  return *this;
}
  • Copy-and-swap:
Foo& Foo::operator=(const Foo& rhs) {
  Foo temp(rhs);  // Use copy constructor in operator=
  swap(temp);  // Then swap
  return *this;
}
Creative Commons License
This blog by Che-Liang Chiou is licensed under a Creative Commons Attribution 4.0 International License.