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. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
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.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Move Constructors and Move Assignment” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Move Constructors and Move Assignment”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
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