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; } |
====== 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.
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.
Contact Us