Move assignment operator: Difference between revisions

Content deleted Content added
Monospace
 
(33 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 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.
 
__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 12 ⟶ 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 ResourceString {
public:
 
String& operator=(String&& otherStringother) noexcept {
public:
// If we're not trying to move the object into itself...
 
Resource&if operator(this !=(Resource && other) {
delete[] this->data_; // Free this string's original data.
if (this->data_ != &other) { .data_; // IfCopy the objectother isnstring'ts beingdata calledpointer oninto this itselfstring.
other.data_ = delete this->datanullptr; // DeleteFinally, reset the objectother string's data pointer.
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
}
return *this;
}
 
private:
void* data;
char* textdata_;
 
};
</syntaxhighlight>A more practical example:<syntaxhighlight lang="c++">
class String {
 
==References==
public:
<references />
 
{{C++ programming language}}
String& operator=(String&& otherString) {
if (this != &otherString) {
delete text;
this->text = otherString.text;
otherString.text = nullptr;
}
return *this;
}
 
[[Category:C++]]
char* text;
[[Category:Operators (programming)]]
[[Category:Assignment operations]]
 
{{compu-prog-stub}}
};
</syntaxhighlight>
==References==
<references />