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

