Move Assignment Operator
   HOME

TheInfoList



OR:

In the
C++ programming language C (''pronounced like the letter c'') is a general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of ...
, the move assignment operator = is used for transferring a temporary object to an existing object. The move assignment operator, like most C++ operators, can be overloaded. Like the
copy assignment operator Copy may refer to: *Copying or the product of copying (including the plural "copies"); the duplication of information or an artifact **Cut, copy and paste, a method of reproducing text or other data in computing **File copying **Photocopying, a pr ...
it is a 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/
move constructor In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required ...
s,
copy assignment operator Copy may refer to: *Copying or the product of copying (including the plural "copies"); the duplication of information or an artifact **Cut, copy and paste, a method of reproducing text or other data in computing **File copying **Photocopying, a pr ...
or destructors have not been declared. The parameter of a move assignment operator is an
rvalue reference C++11 is a version of the International Organization for Standardization, ISO/International Electrotechnical Commission, IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and ...
(T&&) to type ''T'', where ''T'' is 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 is called on an object created by the operation. Thereafter, the other object's data is no longer valid.


Overloading move assignment operator

To overload the move assignment operator, the signature of the function must be: T& operator=(T&& data) To successfully overload the move assignment operator, the following conditions must be met: * Check if the object calling the operator is not calling the operator on itself. * The current object's data is de-allocated. * The object that is being moved from must have its data marked as
nullptr C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions b ...
(or something to signify the move) * The operator must return a reference to "*this". Consider the following move assignment operator for a simple string class: class String ;


References

C++ Operators (programming) Assignment operations {{compu-prog-stub