liveBook · Manning

Move constructor

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

A move constructor is a C++ special member function that initializes an object by transferring resources from a source object.

A move constructor transfers resource ownership from a source object instead of copying the resource. It addresses a practical mistake: copying a resource-heavy object can waste work, while leaving both objects pointing at the same resource can cause incorrect access or leaks. section 2.1 section 7.6

A typical move constructor follows this sequence:

  • Accept an rvalue reference, written with &&, so temporary or explicitly movable objects can provide their resources. section 2.1
  • Transfer the resource handles to the destination rather than allocating and copying the underlying data. section 2.1 section 7.6
  • Neutralize the source, commonly by setting pointers to nullptr, so its former resource cannot be accessed or destroyed incorrectly. section 2.1
  • Coordinate the move constructor with the move assignment operator, copy operations, and destructor when the class owns dynamic resources. section 7.6

Moving a resource-owning Page

A copying constructor allocates a new TextSection for p2. A move constructor instead takes ownership of p1's pointers: Page(Page&& o) : headers(o.headers), body(o.body) { o.headers = nullptr; o.body = nullptr; }. Calling Page p2 = std::move(p1) transfers the resources and leaves p1 neutralized. With std::unique_ptr, the same move operations can be defaulted, reducing manual ownership code. section 2.1 section 7.6

Listing 2.2 A uniqueness-preserving assignment operator with negligible cost
class TextSection {
    // assume a clever implementation
};
 
class Page {
private:
    TextSection* headers;
    TextSection* body;
public:
    Page(TextSection* h) : headers(h), body(new TextSection()) {}
    Page(const Page& o) : headers(o.headers), body(new            #1
TextSection(*o.body)) {}
    Page(Page&& o) : headers(o.headers), body(o.body) {           #2
         o.headers = nullptr;                                     #3
         o.body = nullptr;
    }
    ~Page() { delete body; }
};
 
int main() {
    Page p1(new TextSection());
    Page p2 = std::move(p1);                                      #4
    return 0;
}

Compare costly copying with pointer transfer, source neutralization, and explicit std::move. section 2.1

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

How deep it goes
Explains rvalue references, pointer transfer, source neutralization, and coordination with other special members.
Examples
Uses Page pointer ownership, InputReader arrays, and vector emplacement to contrast copying, moving, and direct construction.
What it assumes
Assumes basic constructors, pointers, dynamic resources, copying, assignment, and destruction; introduces rvalue references and move syntax.
What you can do after
Build resource-owning classes with move construction, source neutralization, and compatible copy, assignment, and destruction behavior.
How it connects
Connects move semantics with move assignment, copy construction, copy assignment, destructors, smart pointers, and emplacement.

Also explained in

Compare how 2 books explain move constructor →

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.