Move assignment operator: Difference between revisions

Content deleted Content added
removing and categorizing
ce, rem tag
Line 1:
In the [[C++|C++ programming language]], the move assignment operator <code>=</code>, is used for transferring ownership (moving) of a temporary object to anotheran existing object. The move assignment operator, like most of the other C++ operators, can be [[Operator overloading|overloaded]]. It is one of thea [[special member functions|special member function]].
{{copy edit|date=February 2016}}
 
If the move assignment operator is not explicitly defined, then the [[compiler]] will generategenerates an implicit move assignment operator ([[C++11]] and newer). The parameter of a move assignment operator is an [[rvalue reference]] (T&&) to type ''T'', where ''T'' beingis 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 would beis called on an object being created. Oneby mustthe alsooperation. signify thatThereafter, the other object's data is notno longer valid anymore, and has been moved.
In the [[C++|C++ programming language]], the move assignment operator <code>=</code>, is used for transferring ownership (moving) of a temporary object to another existing object. The move assignment operator, like most of the other C++ operators, can be [[Operator overloading|overloaded]]. It is one of the [[special member functions]].
 
If the move assignment operator is not explicitly defined, then the compiler will generate an implicit move assignment operator ([[C++11]] and newer). The parameter of a move assignment operator is an [[rvalue reference]] (T&&) to type ''T'', ''T'' being 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 would be called on an object being created. One must also signify that the other object's data is not valid anymore, and has been moved.
 
__FORCETOC__
 
== Overloading move assignment operator ==
To overload the move assignment operator, the signature of the function must be as follows:<ref>{{Cite web|title = Move assignment operator - cppreference.com|url = http://en.cppreference.com/w/cpp/language/move_assignment|website = en.cppreference.com|access-date = 2016-02-23}}</ref><syntaxhighlight lang="c++">
T& operator=(T&& data)
</syntaxhighlight>To successfully overload the move assignment operator, the following conditions must be met:
Line 14 ⟶ 12:
* 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 returnsmust return a reference to "*this".
 
An implementation of the move assignment operator:<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++">