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.

Similar Reads

Move Assignment Operator

The move assignment operator was added in C++ 11 to further strengthen the move semantics in C++. It is like a copy assignment operator but instead of copying the data, this moves the ownership of the given data to the destination object without making any additional copies. The source object is left in a valid but unspecified state....

User-Defined Move Assignment Operator

The programmer can define the move assignment operator using the syntax given below:...

Example of Move Assignment Operator

In this program, we will create a dynamic array class and create a user-defined move assignment operator....

Need of Move Assignment Operator

...

Contact Us