Content deleted Content added
No edit summary |
Monospace |
||
(36 intermediate revisions by 25 users not shown) | |||
Line 1:
{{Short description|Operator used in C++}}
In the [[C++|C++ programming language]], the '''move assignment operator''' <code>=</code> is used for transferring a temporary object to an existing object. The move assignment operator, like most C++ operators, can be [[Operator overloading|overloaded]]. Like the [[copy assignment operator]] it is a [[special member functions|special member function]].
If the move assignment operator is not explicitly defined,
== Overloading move assignment operator ==
To overload the move assignment operator, the signature of the function must be
T& operator=(T&& data)
</syntaxhighlight>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
Consider the following move assignment operator for a simple string class:<ref>{{Cite web|title = Move Constructors and Move Assignment Operators (C++)|url = https://msdn.microsoft.com/en-us/library/dd293665.aspx|website = msdn.microsoft.com|access-date = 2016-02-23}}</ref><syntaxhighlight lang="c++">
class
public:▼
▲public:
// If we're not trying to move the object into itself...
delete[] this->data_; // Free this string's original data.
other.data_ =
}
private:
};
</syntaxhighlight
==References==▼
<references />▼
{{C++ programming language}}
▲ String& operator=(String&& otherString) {
▲ return *this;
[[Category:C++]]
▲ char* text;
[[Category:Operators (programming)]]
[[Category:Assignment operations]]
{{compu-prog-stub}}
▲==References==
▲<references />
|