liveBook · Manning

Move assignment operator

As explained in 100 C++ Mistakes and How to Avoid Them

A move assignment operator is a C++ special member function that transfers resources from a source object into an already existing target object.

A move assignment operator transfers resources from a source object into an already constructed target. Unlike copying, it gives the target ownership of existing data and leaves the source neutralized. The common mistake is treating this as ordinary member-wise assignment, which can leak resources, duplicate ownership, or leave dangling pointers. section 2.1 section 7.6

A safe move assignment should account for the following:

  • Release or otherwise handle resources already owned by the target before taking ownership from the source. section 8.5
  • Transfer pointers or other resource handles instead of copying the owned data. section 2.1
  • Neutralize the source, commonly by setting transferred pointers to nullptr and resetting related state. section 2.1 section 8.5
  • Handle self-assignment and remember that providing one move member can suppress compiler generation of other special members. section 8.5

Replacing an InputReader's resources

A move assignment for InputReader first checks that the source and target differ, deletes the target's filename array, takes the source array and count, then sets the source pointer to nullptr and its count to zero. The target now owns the data, while the source cannot accidentally release or reuse it. section 8.5

    InputReader(InputReader&& r) : filenames(r.filenames), count(r.count) {
        r.filenames = nullptr;
        r.count = 0;
    }
 
 
    InputReader& operator=(InputReader&& r) {
        if (this != &r) {
            delete[] filenames;
            filenames = r.filenames;
            count = r.count;
            r.filenames = nullptr;
            r.count = 0;
        }
        return *this;
    }

The listing provides a compact implementation of move construction and move assignment, including target cleanup, ownership transfer, self-assignment protection, and source neutralization. section 8.5

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Explains target cleanup, resource transfer, source neutralization, self-assignment, and compiler-generation caveats.
Examples
Uses Page and InputReader examples with raw pointers, dynamic arrays, nullptr resets, and defaulted move members.
What it assumes
Assumes understanding of constructors, assignment operators, destructors, dynamic resources, and the distinction between initialized and uninitialized objects.
What you can do after
Build it from scratch by transferring resources, cleaning the target, and leaving the source safely neutralized.
How it connects
Connects move assignment with move construction, copy assignment, copy construction, destructors, and resource ownership.

Also explained in

Compare how 2 books explain move assignment operator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.