How to Implement Move Assignment Operator in C++?
Last Updated :
15 Feb, 2024
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;
}
OutputObject 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)
Similar Reads
How to Create Custom Assignment Operator in C++? In C++, operator overloading allows us to redefine the behavior of an operator for a class. In this article, we will learn how to create a custom assignment operator for a class in C++. Creating Custom Assignment (=) Operator in C++To create a custom assignment operator, we need to define an overloa
2 min read
Double Address Operator in C++ In C++, the double address operator (&&) is used to denote an rvalue reference, which is a type of reference that can bind to temporary objects. In this article, we will learn how to use the double address operator in C++. Double Reference Operator in C++ To understand the double address ope
3 min read
How to Replace an Element in a Vector in C++? In this article, we will learn how to replace an element in a vector in C++.The most efficient method to replace the old value with a new value is by using replace() method. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1,
2 min read
How to Use Bit Manipulation Methods in C++ Bit manipulation is a technique used for optimizing performance and memory usage in various programming scenarios. It is very useful from a Competitive Programming point of view. In this article, we will learn how we can use bit manipulation methods in C++ and provide examples to help you understand
5 min read
How to Insert an Element in a Sorted Vector in C++? In C++, inserting element in a sorted vector should be done such that it preserves the order of elements. In this article, we will learn different methods to insert an element in a sorted vector in C++.The recommended way to insert an element is to first find the position of insertion using lower_bo
4 min read