diff --git a/docs/cpp/move-constructors-and-move-assignment-operators-cpp.md b/docs/cpp/move-constructors-and-move-assignment-operators-cpp.md index 9cbab78ea2..b00f547676 100644 --- a/docs/cpp/move-constructors-and-move-assignment-operators-cpp.md +++ b/docs/cpp/move-constructors-and-move-assignment-operators-cpp.md @@ -125,7 +125,7 @@ The following procedures describe how to write a move constructor and a move ass } ``` -1. In the move assignment operator, add a conditional statement that performs no operation if you try to assign the object to itself. +1. In the move assignment operator, we need to keep the self-assignment safe, and the simple way is to add a judgment directly. ```cpp if (this != &other) @@ -133,6 +133,12 @@ The following procedures describe how to write a move constructor and a move ass } ``` + There are many other ways, too, such as `copy-and-swap`. + + ```cpp + shared_ptr(_Right).swap(*this); + ``` + 1. In the conditional statement, free any resources (such as memory) from the object that is being assigned to. The following example frees the `_data` member from the object that is being assigned to: