Move 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
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.
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.
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.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Incrível!
Completion taxa melhorada para 10
Move Assignment Operators
Deslize para mostrar o menu
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
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.
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.
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.
Obrigado pelo seu feedback!