Move assignment operator
In the C++ programming language, the move assignment operator = is used for transferring a temporary object to an existing object. The move assignment operator, like most C++ operators, can be overloaded. Like the copy assignment operator it is a special member function.
If the move assignment operator is not explicitly defined, the compiler generates an implicit move assignment operator (C++11 and newer) provided that copy/move constructors, copy assignment operator or destructors have not been declared.[1] The parameter of a move assignment operator is an rvalue reference (T&&) to type T, where T is the object that defines the move assignment operator. The move assignment operator is different than a move constructor because a move assignment operator is called on an existing object, while a move constructor is called on an object created by the operation. Thereafter, the other object's data is no longer valid.
Overloading move assignment operator
To overload the move assignment operator, the signature of the function must be:[1]
T& operator=(T&& data);
To successfully overload the move assignment operator, the following conditions must be met:
- Check if the object calling the operator is not calling the operator on itself.
- The current object's data is de-allocated.
- The object that is being moved from must have its data marked as
nullptr(or something to signify the move) - The operator must return a reference to
*this.
Consider the following move assignment operator for a simple string class:[2]
class MyString {
private:
char* data_;
public:
MyString& operator=(MyString&& other) noexcept {
// If we're not trying to move the object into itself...
if (this != &other) {
delete[] this->data; // Free this string's original data.
this->data = other.data; // Copy the other string's data pointer into this string.
other.data = nullptr; // Finally, reset the other string's data pointer.
}
return *this;
}
};
References
- ^ a b "Move assignment operator - cppreference.com". en.cppreference.com. Retrieved 2016-02-23.
- ^ "Move Constructors and Move Assignment Operators (C++)". msdn.microsoft.com. Retrieved 2016-02-23.
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.