Assignment
The same considerations that make move semantics appropriate for constructors make them appropriate for assignment. Here, for example, is how you could code the copy assignment and the move assignment operators for the Useless class:
Useless & Useless::operator=(const Useless & f) // copy assignment{ if (this == &f) return *this; delete [] pc; n = f.n; pc = new char[n]; for (int i = 0; i < n; i++) pc[i] = f.pc[i]; return *this;}Useless & Useless::operator=(Useless && f) // move assignment{ if (this == &f) return *this; delete [] pc; n = f.n; pc = f.pc; f.n = 0; f.pc = nullptr; return *this;}
The copy assignment operator follows the usual pattern given in Chapter 12 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access