User-Defined Move Assignment Operator
The programmer can define the move assignment operator using the syntax given below:
MyClass& operator= (MyClass&& otherObf) noexcept { // Take resources from 'other' and make them our own // Properly release our resources if needed // ... return *this; }
As you may have noticed, the move assignment operator function uses a special && reference qualifier. It represents the r-value references (generally literals or temporary values).
Usually, it returns a reference to the object (in this case, *this) so you can chain assignments together.
The move constructor is called by the compiler when the argument is an rvalue reference which can be done by std::move() function.
Move Assignment Operator in C++ 11
In C++ programming, we have a feature called the move assignment operator, which was introduced in C++11. It helps us handle objects more efficiently, especially when it comes to managing resources like memory. In this article, we will discuss move assignment operators, when they are useful and called, and how to create user-defined move assignment operators.