Advertisement
Ginsutime

Just Move Assignment Operator Cherno

Feb 19th, 2022
1,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1.     String& operator=(String&& other) noexcept
  2.     {   // Not constructing new object like in the move constructor above, we're moving another object into this current object
  3.         // Meaning we need to overwrite the current object
  4.         printf("Moved!\n");
  5.  
  6.         if (this != &other) // Making sure we can't move the current object into itself. AKA same object.
  7.         {
  8.             delete[] m_Data; // Have to add this because we're about to move another object into ourselves and the old data has to be deleted
  9.  
  10.             m_Size = other.m_Size;
  11.             m_Data = other.m_Data;
  12.  
  13.             other.m_Size = 0;
  14.             other.m_Data = nullptr;
  15.         }
  16.  
  17.         return *this; // reference to current object
  18.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement