Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Move Assignment Operators | Implementing Move Operations
C++ Move Semantics

bookMove Assignment Operators

The move assignment operator transfers resources from an rvalue to an existing object, releasing any resources the target already owns. You use it to efficiently "move" ownership of dynamically allocated memory or other resources from a temporary or expiring object to another, instead of copying. The operator must handle self-assignment and ensure no resource leaks. This is especially important when working with classes that manage their own resources, such as dynamic arrays or file handles.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
#include <iostream> #include <cstring> class String { char* data; public: // Constructor String(const char* str = "") { data = new char[std::strlen(str) + 1]; std::strcpy(data, str); std::cout << "Constructed: " << data << "\n"; } // Destructor ~String() { delete[] data; std::cout << "Destroyed\n"; } // Move constructor String(String&& other) noexcept : data(other.data) { other.data = nullptr; std::cout << "Move constructed\n"; } // Move assignment operator String& operator=(String&& other) noexcept { if (this != &other) { // Self-assignment check delete[] data; // Release current resource data = other.data; // Transfer ownership other.data = nullptr; // Reset source std::cout << "Move assigned\n"; } return *this; } // Print method void print() const { if (data) std::cout << "String: " << data << "\n"; else std::cout << "String: (null)\n"; } }; int main() { String a("Hello"); String b("World"); a = std::move(b); // Move assignment a.print(); b.print(); a = std::move(a); // Self-assignment test a.print(); }

The move assignment operator above first checks for self-assignment, then releases any owned resources before taking ownership from the source object. After moving, the source object's pointer is set to nullptr to avoid double deletion. This ensures that resources are not leaked or deleted twice, and that the target object now owns the resource previously held by the source.

Why check for self-assignment?
expand arrow

Self-assignment can occur in rare cases (such as a = std::move(a)). The check prevents deleting the resource before moving it, which would result in undefined behavior or a crash.

What should you do with the source object?
expand arrow

After moving, set the source's pointers to nullptr or reset its state to avoid double deletion. This ensures safe cleanup and prevents resource management errors.

question mark

Why is it important to check for self-assignment in a move assignment operator?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 2

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you show me an example implementation of a move assignment operator?

What is the difference between move assignment and copy assignment?

Why is it important to set the source pointer to nullptr after moving?

bookMove Assignment Operators

Veeg om het menu te tonen

The move assignment operator transfers resources from an rvalue to an existing object, releasing any resources the target already owns. You use it to efficiently "move" ownership of dynamically allocated memory or other resources from a temporary or expiring object to another, instead of copying. The operator must handle self-assignment and ensure no resource leaks. This is especially important when working with classes that manage their own resources, such as dynamic arrays or file handles.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
#include <iostream> #include <cstring> class String { char* data; public: // Constructor String(const char* str = "") { data = new char[std::strlen(str) + 1]; std::strcpy(data, str); std::cout << "Constructed: " << data << "\n"; } // Destructor ~String() { delete[] data; std::cout << "Destroyed\n"; } // Move constructor String(String&& other) noexcept : data(other.data) { other.data = nullptr; std::cout << "Move constructed\n"; } // Move assignment operator String& operator=(String&& other) noexcept { if (this != &other) { // Self-assignment check delete[] data; // Release current resource data = other.data; // Transfer ownership other.data = nullptr; // Reset source std::cout << "Move assigned\n"; } return *this; } // Print method void print() const { if (data) std::cout << "String: " << data << "\n"; else std::cout << "String: (null)\n"; } }; int main() { String a("Hello"); String b("World"); a = std::move(b); // Move assignment a.print(); b.print(); a = std::move(a); // Self-assignment test a.print(); }

The move assignment operator above first checks for self-assignment, then releases any owned resources before taking ownership from the source object. After moving, the source object's pointer is set to nullptr to avoid double deletion. This ensures that resources are not leaked or deleted twice, and that the target object now owns the resource previously held by the source.

Why check for self-assignment?
expand arrow

Self-assignment can occur in rare cases (such as a = std::move(a)). The check prevents deleting the resource before moving it, which would result in undefined behavior or a crash.

What should you do with the source object?
expand arrow

After moving, set the source's pointers to nullptr or reset its state to avoid double deletion. This ensures safe cleanup and prevents resource management errors.

question mark

Why is it important to check for self-assignment in a move assignment operator?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 2
some-alt