Step 3 of 7
Move assignment and the rule of five
Move assignment (a = std::move(b);) must also release what a currently owns, and must survive self-assignment:
Buffer& operator=(Buffer&& other) noexcept {
if (this != &other) {
delete[] data_;
data_ = other.data_; size_ = other.size_;
other.data_ = nullptr; other.size_ = 0;
}
return *this;
}
The rule of five: if you write any of destructor, copy constructor, copy assignment, move constructor, move assignment, think about all five. Even better is the rule of zero: use members like std::vector and std::unique_ptr that already handle all five, and write none.
Your turn: add move assignment.
Previous: Write a move constructor Next: Return by value is free