0
0
CppConceptBeginner · 4 min read

Move Assignment Operator in C++: What It Is and How It Works

The move assignment operator in C++ is a special operator that transfers ownership of resources from one object to another without copying. It allows efficient reuse of resources by 'moving' them, typically implemented with operator= taking an rvalue reference parameter.
⚙️

How It Works

The move assignment operator works by taking the resources (like memory or file handles) from one object and giving them to another, instead of making a full copy. Imagine you have a box of toys, and instead of making a new box and copying each toy, you just hand over the whole box to your friend. This saves time and effort.

In C++, this is done using an rvalue reference parameter (usually Type&&). The operator steals the internal data from the source object and then leaves the source in a safe, empty state. This avoids expensive copying and improves performance, especially for objects managing dynamic memory or other heavy resources.

💻

Example

This example shows a simple class with a move assignment operator that transfers ownership of a dynamic array.

cpp
#include <iostream>
#include <utility> // for std::move

class Buffer {
    int* data;
    size_t size;
public:
    Buffer(size_t s) : size(s), data(new int[s]) {
        std::cout << "Constructed Buffer of size " << size << "\n";
    }
    ~Buffer() { delete[] data; }

    // Move assignment operator
    Buffer& operator=(Buffer&& other) noexcept {
        if (this != &other) {
            delete[] data; // free current resource
            data = other.data; // steal resource
            size = other.size;
            other.data = nullptr; // leave other empty
            other.size = 0;
            std::cout << "Move assigned Buffer\n";
        }
        return *this;
    }

    void print() const {
        std::cout << "Buffer size: " << size << "\n";
    }
};

int main() {
    Buffer buf1(5);
    Buffer buf2(10);
    buf2 = std::move(buf1); // move assignment
    buf2.print();
    buf1.print();
    return 0;
}
Output
Constructed Buffer of size 5 Constructed Buffer of size 10 Move assigned Buffer Buffer size: 5 Buffer size: 0
🎯

When to Use

Use the move assignment operator when you want to efficiently transfer resources from one object to another without copying. This is especially useful for classes that manage dynamic memory, file handles, or network connections.

For example, when returning large objects from functions or when storing objects in containers, move assignment avoids costly copies and improves performance. It is also helpful when implementing classes that follow the Rule of Five, ensuring safe and efficient resource management.

Key Points

  • The move assignment operator transfers ownership of resources instead of copying.
  • It takes an rvalue reference parameter (Type&&).
  • After moving, the source object is left in a valid but empty state.
  • It improves performance by avoiding expensive deep copies.
  • Commonly used in classes managing dynamic resources like memory or files.

Key Takeaways

The move assignment operator efficiently transfers resources without copying.
It uses an rvalue reference parameter to 'steal' resources from the source object.
After moving, the source object should be left in a safe, empty state.
Use move assignment to improve performance in resource-managing classes.
Implement move assignment when following the Rule of Five in C++.