In the
C++ programming language, 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 it is a
special member function.
If the move assignment operator is not explicitly defined, the
compiler
In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
generates an implicit move assignment operator (
C++11
C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior vers ...
and newer) provided that
copy/
move constructors,
copy assignment operator or
destructors have not been declared.
[ The parameter of a move assignment operator is an rvalue reference () 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 (or something to signify the move)
* The operator must return a reference to "".
Consider the following move assignment operator for a simple string class:
class String ;
References
C++
Operators (programming)
Assignment operations
{{compu-prog-stub