C/C++ Arena

Step 2 of 7

Write a move constructor

A class that owns a resource can implement moving itself. The move constructor takes an rvalue reference (T&&), steals the pointer, and leaves the source empty so its destructor does nothing harmful:

Buffer(Buffer&& other) noexcept
    : data_(other.data_), size_(other.size_) {
    other.data_ = nullptr;
    other.size_ = 0;
}

noexcept promises it won't fail, which lets std::vector use it when growing.

Your turn: write the move constructor for Buffer.

Previous: Copies are expensive Next: Move assignment and the rule of five