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).
- Copy-and-swap: