1. Problem It Solves
Move construction initializes a new object by taking resources, while move assignment replaces an existing object's resources with those from another object. It makes an important constraint visible instead of leaving readers to guess. This lesson keeps only the C++11 core that fits one focused day.
2. Prerequisites
The ideas from Day 18, plus basic variables, functions, and output already introduced.
3. Core Idea
Mental model: Move construction initializes a new object by taking resources, while move assignment replaces an existing object's resources with those from another object. Identify the relevant value or state, who owns it, and whether the rule acts during compilation or execution.
4. Minimal Syntax
Owner(Owner&& other); Owner& operator=(Owner&& other);5. How It Works
The example creates a tiny fixed state with no keyboard input.
C++11 or the standard-library contract applies today's rule.
The program prints the important result so it can be checked against the source.
6. Common Mistakes
A move operation that forgets to neutralize the source can make both objects release the same resource.
7. When to Use It
Use it when a type directly owns a resource and copying would be expensive.
Avoid it when it hides ownership, lifetime, type, ordering, or cost.
8. Simple Example
A tiny owner transfers one dynamic integer through move construction and then move assignment, leaving each source safely empty. The .cpp keeps the data fixed and avoids unrelated abstraction.
Complete sample code
Source file
cpp11/19_move_constructor_move_assignment/main.cpp
#include <iostream>
#include <utility>
class IntOwner {
public:
explicit IntOwner(int value) : data_(new int(value)) {}
~IntOwner() { delete data_; }
IntOwner(IntOwner&& other) : data_(other.data_) {
other.data_ = nullptr;
std::cout << "move-constructed\n";
}
IntOwner& operator=(IntOwner&& other) {
if (this != &other) {
delete data_;
data_ = other.data_;
other.data_ = nullptr;
}
std::cout << "move-assigned\n";
return *this;
}
int get() const { return data_ ? *data_ : -1; }
private:
IntOwner(const IntOwner&);
IntOwner& operator=(const IntOwner&);
int* data_;
};
int main() {
IntOwner first(7);
IntOwner second(std::move(first));
IntOwner third(1);
third = std::move(second);
std::cout << "first=" << first.get() << " third=" << third.get() << '\n';
}
9. Key Takeaways
The feature is part of the C++11 scope used in this course.
Understand its lifetime, ownership, type, and ordering consequences.
Compile with warnings and prefer the smallest form that makes the rule obvious.
10. Self-Check Questions
Easy — When are
Owner(Owner&& other)andOwner& operator=(Owner&& other)used? When transferring an owning pointer, what must happen to the destination's old resource and the source?Medium — Read the small example described above. What value or state should it print, and which rule produces that result?
Hard — Find and explain the subtle bug in this situation: A move operation that forgets to neutralize the source can make both objects release the same resource. What is the smallest C++11-safe correction?