Content deleted Content added
m v2.04b - Bot T20 CW#61 - Fix errors for CW project (Reference before punctuation) |
Monospace |
||
(4 intermediate revisions by 3 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, the [[compiler]] generates an implicit move assignment operator ([[C++11]] and newer) provided that [[Copy constructor (C++)|copy]]/[[
== Overloading move assignment operator ==
Line 10 ⟶ 11:
* 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 "{{Mono|*this}}".
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++">
Line 18 ⟶ 19:
// If we're not trying to move the object into itself...
if (this != &other) {
delete[] this->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.
}
Line 32 ⟶ 33:
==References==
<references />
{{C++ programming language}}
[[Category:C++]]
|