Move assignment operator: Difference between revisions

Content deleted Content added
stub-sort
Monospace
 
(30 intermediate revisions by 23 users not shown)
Line 1:
{{Short description|Operator used in C++}}
{{copy edit|date=February 2016}}
In the [[C++|C++ programming language]], the '''move assignment operator''' (<code>=),</code> is used for transferring ownershipa (moving)temporary ofobject anto an already instantiated object or resource to anotherexisting object. The move assignment operator, like most of the other C++ operators, can be [[Operator overloading|overloaded]]. It is one the ofLike the [[specialcopy memberassignment functionsoperator]].<ref>{{Cite journal|titleit =is Speciala [[special member functions|urlspecial =member https://enfunction]].wikipedia.org/w/index.php?title=Special_member_functions&oldid=662581955|journal = Wikipedia, the free encyclopedia|language = en}}</ref>
 
If the move assignment operator is not explicitly defined, then the [[compiler]] will generategenerates an implicit move assignment operator ([[C++11]] and newer) provided that [[Copy constructor (C++)|copy]]/[[move constructor]]s, [[Assignment operator (C++)|copy assignment operator]] or [[Destructor (computer programming)|destructors]] have not been declared.<ref name=":0" /> The parametersparameter of a move assignment operator areis an [[rvalue reference]] ({{Mono|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, aswhile a move constructor would beis called on an object being created. Oneby mustthe somehowoperation. also signifyThereafter, the other object's data is notno longer valid anymore, and has been moved.
In the [[C++|C++ programming language]], the move assignment operator (=), is used for transferring ownership (moving) of an an already instantiated object or resource to another object. The move assignment operator, like most of the other C++ operators, can be [[Operator overloading|overloaded]]. It is one the of the [[special member functions]].<ref>{{Cite journal|title = Special member functions|url = https://en.wikipedia.org/w/index.php?title=Special_member_functions&oldid=662581955|journal = Wikipedia, the free encyclopedia|language = en}}</ref>
 
If the move assignment operator is not explicitly defined, then the compiler will generate an implicit move assignment operator ([[C++11]] and newer). The parameters of a move assignment operator are 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, as a move constructor would be called on an object being created. One must somehow also signify 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 name=":0">{{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 ⟶ 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 returnsmust return a reference to "{{Mono|*this}}".
 
An implementation ofConsider 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 Resource {
 
public:
 
Resource& operator=(Resource&& other) {
if (this != &other) { // If the object isn't being called on itself
delete this->data; // Delete the object's data
this->data = other.data; // "Move" other's data into the current object
other.data = nullptr; // Mark the other object as "empty"
}
return *this; // return *this
}
 
void* data;
 
};
</syntaxhighlight>A more practical example:<syntaxhighlight lang="c++">
class String {
public:
 
ResourceString& operator=(ResourceString&& other) noexcept {
public:
// If we're not trying to move the object into itself...
 
String&if operator(this !=(String && otherStringother) {
delete[] this->datadata_; // DeleteFree thethis objectstring's original data.
this->datadata_ = other.datadata_; // "Move"Copy the other string's data pointer into thethis current objectstring.
if (this != &otherString) {
other.datadata_ = nullptr; // MarkFinally, reset the other objectstring's asdata "empty"pointer.
delete text;
this->text = otherString.text;
otherString.text = nullptr;
}
return *this;
}
return *this;
}
 
private:
char* textdata_;
 
};
</syntaxhighlight>
 
==References==
<references />
 
{{C++ programming language}}
{{uncategorised|date=February 2016}}
 
[[Category:C++]]
[[Category:Operators (programming)]]
[[Category:Assignment operations]]
 
{{compu-prog-stub}}