The copy and swap idiom in C++
Post C++11, it is probably better to use the &quot;copy and move&quot; idiom.<p><pre><code>    MyClass&amp; operator=(const MyClass&amp; other) {

      return (*this) = MyClass(other);
    }</code></pre>