Move Constructors and Move Assignment
Implement move constructors and move assignment operators correctly.
Move Constructors and Move Assignment is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Special Members
The move constructor and move assignment operator let your class take ownership of another instance s resources without copying.
Signature
Both take an rvalue reference to the same type.
class String {
public:
String(String&& other) noexcept; // move ctor
String& operator=(String&& other) noexcept; // move assign
};Implementing a Move Constructor
Steal the source s resources and leave it in a valid empty state.
String(String&& other) noexcept
: data_(other.data_), size_(other.size_) {
other.data_ = nullptr;
other.size_ = 0;
}Implementing Move Assignment
Three steps: release current resources, steal the source s, leave the source empty.
String& operator=(String&& other) noexcept {
if (this != &other) {
delete[] data_;
data_ = other.data_;
size_ = other.size_;
other.data_ = nullptr;
other.size_ = 0;
}
return *this;
}Why noexcept Matters
Mark move operations noexcept whenever possible. Standard containers like std::vector only use moves on resize if they are noexcept; otherwise they fall back to copies for exception safety.
The Rule of Five
If you write any of these five, think about all five:
- Destructor
- Copy constructor
- Copy assignment
- Move constructor
- Move assignment
The Rule of Zero
Even better — write none of them. Store members as RAII types (vectors, strings, smart pointers). Compiler-generated defaults work correctly and efficiently.
class Widget {
std::string name_;
std::vector<int> data_;
// No need for destructor, copy, or move — compiler does the right thing
};When the Compiler Generates Moves
The compiler generates default move members only when:
- No user-declared copy constructor, copy assignment, destructor, or move members
- All members and bases are themselves movable
Defaulted and Deleted Moves
You can request or forbid the compiler-generated moves explicitly.
class Widget {
public:
Widget(Widget&&) = default;
Widget& operator=(Widget&&) = default;
// or
Widget(Widget&&) = delete;
};After a Move
The moved-from object is in a valid but unspecified state. You can:
- Destroy it
- Assign a new value to it
- Call any operation that has no precondition (size(), empty(), clear())
Use Cases
Move semantics speeds up:
- Returning large objects (when RVO does not kick in)
- Putting big objects into containers (push_back of rvalues)
- Implementing your own resource-owning types
Quick Check
Why should move constructors and move assignment operators be marked noexcept?
Recap
Move constructors and move assignment operators steal resources from a soon-to-die rvalue. Mark them noexcept for container speed. Follow the Rule of Five — or better, the Rule of Zero by composing standard RAII types.
Frequently asked questions
Is the “Move Constructors and Move Assignment” lesson free?
Yes — the full text of “Move Constructors and Move Assignment” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Move Constructors and Move Assignment”?
Implement move constructors and move assignment operators correctly. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Move Constructors and Move Assignment” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Lvalue vs Rvalue The Distinction
- Rvalue References and the && Syntax
- std::move and std::forward
- Move Constructors and Move Assignment