Part III Using swap When using moveFrom , if a new data member is added, both swap and moveFrom have to be modified. To avoid that, one can write the move constructor and move assignment operator with a default constructor and the swap function. class Bank { private : Bank ( ) = default ; } Now cleanup() and moveFrom() can be removed. The destructor handles the code from cleanup . The code is as follows: Bank :: Bank ( Bank && src ) noexcept : Bank ( ) { swap ( * this , src ) ; //Default constructor creates *this. } Bank & Bank :: operator = ( Bank && rhs ) noexcept { Bank temp ( std :: move ( rhs ) ) //Need std::move because rhs is named swap ( * this , temp ) ; return * this ; } Implementing swap with move semantics Using move semantics improves the performance of swap as seen below: void swa...
Comments
Post a Comment