Move Assignment Operator in C++ 11
Last Updated :
13 Oct, 2023
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.
Similar Reads
C++ Programming Language
C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Object Oriented Programming in C++
Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Inheritance in C++
The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read