Move assignment operator: Difference between revisions

Content deleted Content added
Remove a redundant example
C++Bro123 (talk | contribs)
Line 14:
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 String {
public:
char* data;
String& operator=(String&& other) {
 
// If we're not trying to move the object into itself...
public:
String&if operator(this !=(String && other) {
delete if (this != &other) { ->data_; // If we're not trying to moveDelete the objectstring's intooriginal itself..data.
delete this->data; data_ = other.data_; // DeleteCopy the other string's originaldata into datathis string.
this->dataother.data_ = other.datanullptr; // CopyFinally, reset the other string's data into this stringpointer.
other.data = nullptr; // Finally, reset the other string's data pointer.
}
return *this;
}
return *this;
}
 
private:
char* datadata_;
};
</syntaxhighlight>
 
==References==
<references />