Open In App

How to Implement Move Assignment Operator in C++?

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C+, the move assignment operator is used to transfer the ownership of the resources from one object to another when the object is assigned some rvalue references or using std::move. It allows efficient memory manipulation by avoiding unnecessary copies of the objects. In this article, we will learn how to implement a move assignment operator in C++.

Implement Move Assignment Operator in C++

The move assignment operator also overloads the assignment operator in its implementation like the copy assignment operator. The difference is in the type of argument. The move assignment operator takes the rvalue reference as its argument. To define the move assignment operator use the below syntax:

Syntax to Define Move Assignment Operator

className& operator= (className&& other) noexcept {
//Resources are taken from 'other' and make them our own
//Properly release our resources

return *this;
}

Here, className is the name of our class and && indicates that other is an rvalue reference that allows the function to bind to temporary objects.

C++ Program to Implement Move Assignment Operator

The below example demonstrates the implementation of the move assignment operator.

C++




// C++ program demonstrate how to implement move assignment
// operator in class
#include <iostream>
#include <utility>
  
using namespace std;
  
class MyClass {
private:
    int* data;
    size_t size;
  
public:
    MyClass(size_t n)
        : size(n)
    {
        data = new int[size];
        for (size_t i = 0; i < size; ++i) {
            data[i] = static_cast<int>(i);
        }
    }
  
    // Destructor
    ~MyClass() { delete[] data; }
  
    // Move assignment operator
    MyClass& operator=(MyClass&& other) noexcept
    {
        if (this != &other) {
            // Release our own resources
            delete[] data;
  
            // Transfer ownership from 'other' to 'this'
            data = other.data;
            size = other.size;
  
            // Reset 'other' to a valid state
            other.data = nullptr;
            other.size = 0;
        }
        return *this;
    }
  
    // Print data
    void printData() const
    {
        for (size_t i = 0; i < size; ++i) {
            cout << data[i] << " ";
        }
        cout << endl;
    }
};
  
int main()
{
    // Create an object with some data
    MyClass obj1(5);
    cout << "Object 1 data: ";
    obj1.printData();
  
    // Create another object and move from obj1
    MyClass obj2(3);
    obj2 = move(obj1); // Move assignment
  
    cout << "Object 2 data after move: ";
    obj2.printData();
  
    // obj1 is now in a valid but unspecified state
    // (its data has been moved to obj2)
  
    return 0;
}


Output

Object 1 data: 0 1 2 3 4 
Object 2 data after move: 0 1 2 3 4 

Time Complexity: O(1)
Space Complexity: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads