Open In App

Move Assignment Operator in C++ 11

Last Updated : 13 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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:

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. 

Example of Move Assignment Operator

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

C++




// C++ program to illustrate the use of user-defined move
// assignment operator
#include <iostream>
#include <memory>
#include <utility>
  
using namespace std;
  
// dynamic array class
class DynamicArray {
private:
    size_t size_;
    int* data_;
  
public:
    // Constructor to create an array of a given size
    DynamicArray(size_t size)
        : size_(size)
        , data_(new int[size])
    {
        for (size_t i = 0; i < size; ++i) {
            data_[i] = 0; // Initialize all elements to 0
        }
    }
  
    // Move assignment operator
    DynamicArray& operator=(DynamicArray&& other) noexcept
    {
        // Make sure we're not assigning to ourselves
        if (this != &other) {
            // Release our current resources
            delete[] data_;
  
            // Take resources from 'other' and make them our
            // own
            size_ = other.size_;
            data_ = other.data_;
  
            // Reset 'other' to a valid but unspecified
            // state
            other.size_ = 0;
            other.data_ = nullptr;
        }
        return *this;
    }
  
    // Destructor to clean up resources
    ~DynamicArray() { delete[] data_; }
  
    // Function to print the array
    void print() const
    {
        for (size_t i = 0; i < size_; ++i) {
            cout << data_[i] << " ";
        }
        cout << endl;
    }
};
  
// driver code
int main()
{
    // Create an array with 5 elements, initially filled
    // with 0s
    DynamicArray arr1(5);
    // Create another array with 10 elements, initially
    // filled with 0s
    DynamicArray arr2(10);
  
    cout << "====== BEFORE MOVING ======" << endl;
  
    // Print the initial state of arr1
    cout << "arr1: ";
    arr1.print();
  
    // Print the initial state of arr2
    cout << "arr2: ";
    arr2.print();
  
    // Let's assign arr1 to arr2 using the move assignment
    // operator
    arr2 = std::move(arr1);
  
    // Print the state of arr1 after the move (it's in a
    // valid but unspecified state)
    cout << endl << "====== AFTER MOVING ======" << endl;
    cout << "arr1: ";
    arr1.print();
  
    // Print the state of arr2 after the move (now contains
    // the resources of arr1)
    cout << "arr2: ";
    arr2.print();
  
    return 0;
}


Output

====== BEFORE MOVING ======
arr1: 0 0 0 0 0 
arr2: 0 0 0 0 0 0 0 0 0 0 

====== AFTER MOVING ======
arr1: 
arr2: 0 0 0 0 0 

Explanation

The move assignment operator (operator=) is used to transfer resources from one DynamicArray object to another. It releases the current resources of the destination object, takes the resources from the source object, and leaves the source object in a valid but unspecified state.

In the main function:

  • We create two DynamicArray objects, arr1 with 5 elements and arr2 with 10 elements. We print their initial states.
  • We use the move assignment operator to transfer the resources from arr1 to arr2 by calling arr2 = std::move(arr1).
  • After the move, we print the states of both arr1 and arr2. arr1 is now in a valid but unspecified state, and arr2 contains the resources of arr1.

Note: We can also define a move constructor with a move assignment operator and reduce the code redundancy by calling the move assignment operator from the move constructor. To know about move constructor, refer to the article – Move Constructors in C++

Implicit Definition of Move Assignment Operator

The compiler also defines a default move assignment operator implicitly when the following conditions are satisfied:

  • No user-defined copy constructor is present.
  • No user-defined destructor is present.
  • No user-defined move constructor is present.

Need of Move Assignment Operator

In C++, objects can manage resources like memory. When we copy an object, it can be quite slow, especially if the object holds a lot of resources. Traditional copy operations create new copies, which can lead to unnecessary memory usage and slow down your program.

This is where move assignment operators come to the rescue. They let one object take over the resources of another, without making extra copies. This can significantly boost performance in scenarios like resizing arrays or returning objects from functions.

In contrast to the copy assignment operator, the end result of the move assignment operator will be a single copy of the source object.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads