Move Constructor in C++
A move constructor is a specific member function in C++ that enables quick and efficient resource transfers from one object to another, usually when handling temporary objects or returning objects from functions. It transfers data from one object (the source) to another (the destination) without making a deep copy, cutting down on the extra work required to copy vast amounts of data.
Syntax:
class MyClass {
Public:
// Here, we are creating a default constructor
MyClass() : data(nullptr) {}
// Now we construct a move constructor
MyClass(MyClass&& other) : data(std::move(other.data)) {
other.data = nullptr;
}
Private:
int* data; // Example member variable (owning a resource)
};
The copy constructors in C++ work with lvalue references and copy semantics, which refers to copying actual object data rather than creating a new object that points to an existing object in a heap. In comparison, move constructors focus on move semantics (pointing to an existing object in memory) and rvalue references.
A temporary object is first formed when a new object is declared and given a rvalue, which is then used to assign values to the new object. As a result, the copy constructor is called numerous times, increasing overhead, and lowering the code's computational efficiency. We employ move constructors to reduce this overhead and improve the efficiency of the function.
What is the Use of Move Constructors?
The move constructor makes the declared object's pointer point to the data of a temporary object and nulls out the pointer of the temporary objects, unlike copy constructors that copy the data of the existing object and assign it to the new object. As a result, the move constructor stops unnecessary memory data copying.
The move constructor's operation resembles that of the standard member-wise copy constructor, except, in this case, it eliminates the temporary object's pointer to prevent multiple objects from pointing to the same memory region.
Example:
// This is an example c++ program without declaring the move constructor
#include <iostream>
#include <vector>
using namespace std;
//class sample
class sample {
private:
int* value;
Public:
//Sample constructor
sample(int var) {
value = new int;
*value = var;
cout << "The Constructor sample is called for the number: " << var << endl;
}
// construct copy constructor with same name of class
sample(const sample& source) : value(new int(*source.value)) {
cout << "The Copy constructor is called: " << *source.value << endl;
}
//Create a deconstructor for the sample
~sample() {
if (value != nullptr)
cout << "The sample Destructor is called for " << *value << endl;
else
cout << "The sample Destructor is called for nullptr" << endl;
delete value;
}
};
// main function
int main() {
// Create vector of sample class
vector<sample> init;
// Now we insert objects into the sample class
init.push_back(sample{ 35 });
init.push_back(sample{ 79 });
return 0;
}
Output:
![Move Constructor in C++](https://static.tutorialandexample.com/cpp/move-constructor-in-cpp1.png)
The above C++ code creates a class sample containing a constructor, copy constructor, and destructor. To hold objects of the sample class, it uses a vector. When elements are added to the vector, the copy constructor is called, which executes deep copies. The destructor deallocates memory when objects leave the scope or are removed from the vector.
The program above demonstrates how to copy constructor is called needlessly, and memory is used inefficiently by creating new objects by repeatedly copying the same data.
How do Move Constructor Functions in C++
In C++11, two new functions are defined to offer move semantics and functionalities: a move constructor and a move assignment operator. The copy constructor and copy assignment try to make a copy of one object and assign it to another, whereas the move constructor and move assignment aim to transfer ownership of the resources from one object to another. The cost of utilizing move semantics is frequently much lower than using a copy constructor to duplicate something.
While the copy semantics of these functions need a const lvalue reference parameter, the move semantics of these functions require non-const rvalue reference parameters.
Unless the class has no defined copy constructors, copy assignments, move assignments, or destructors, a move constructor and move assignment operator won't usually be provided by default.
Example:
#include <iostream>
#include <vector>
using namespace std;
//Create a sample class
class sample {
private:
// declare a pointer named value
int* value;
Public:
//Create a constructor for the sample
sample(int num)
{
// allocate memory for value pointer
value = new int;
*value = num;
// print message to indicate constructor call
cout << "Constructor is called for " << num << endl;
};
// create a copy constructor for sample
sample(const sample& source)
: sample{ *source.value}
{
//Perform deep copy of value pointer
cout << "The sample constructor is called."
<< "Deep copy for."
<< *source.value
<< endl;
}
// create a move constructor for sample
sample(sample&& source)
: value{ source.value }
{
// transfer ownership of value pointer
cout << "The sample Constructor for "
<< *source.value << endl;
// set source value pointer to null
source.value = nullptr;
}
//Create a destructor for the sample
~sample()
{
if (value != nullptr)
// print message to indicate destructor call
cout << "The Destructor sample is called for."
<< *value << endl;
else
// print message to indicate null pointer
cout << "Destructor is called."
<< " for nullptr."
<< endl;
// deallocate memory for value pointer
delete value;
}
};
// main function
int main()
{
//Create vector for sample
vector<sample> init;
// add two objects to the vector using push_back
init.push_back(sample{ 68 });
init.push_back(sample{ 29 });
return 0;
}
Output:
![Move Constructor in C++](https://static.tutorialandexample.com/cpp/move-constructor-in-cpp2.png)
With a constructor, copy constructor, move constructor, and destructor, the above C++ code creates a sample class. A pointer to dynamically allocated memory is present in the class. The code exemplifies deep copying and move semantics when objects are added to a vector. It prints messages to show when each object's constructor and destructor have been called.
Additional Illustrations of Move Constructors
For a better understanding of the move constructors in C++ programs, let's look at some examples of move semantics.
Example 1:
//In the C++ program below, we pass ownership (a pointer) of one object to another using the move constructor.
#include <iostream>
#include <string>
using namespace std;
class sample {
private:
string s;
Public:
sample() : s("Welcome to TutorialsAndExample") // Initializing s to "default string"
{
// Empty default constructor
}
sample(const sample& var) : s(var.s) {
cout << "Copy constructor invoked!\n";
}
sample(sample&& var) : s(move(var.s)) {
cout << "Move constructor invoked!\n";
}
string action() {
return s;
}
};
// Global temp method of type sample (returns sample type object)
sample action(sample g) {
return g;
}
int main() {
// Move constructor from rvalue
sample var1 = action(sample());
// Move constructor from lvalue
cout << "Before move constructor call: var1 = " << var1.action() << endl;
// Calling move constructor on var1
sample var2 = move(var1);
// Ownership of var1 data transferred to var2
cout << "After move constructor call: var1 = " << var1.action() << endl;
cout << "After move constructor call: var2 = " << var2.action() << endl;
return 0;
}
Output:
![Move Constructor in C++](https://static.tutorialandexample.com/cpp/move-constructor-in-cpp3.png)
The above code illustrates C++ move semantics. It defines a sample class with a default, copy, and move constructor. When objects are moved, the move constructor effectively transfers ownership of the string resource, preventing the need for extra copying. The application builds temporary and permanent objects while demonstrating how to utilize the move constructor and transfer ownership.
Example 2:
#include <iostream>
#include <string>
using namespace std;
class sample
{
Private:
string s;
Public:
sample() : s("Welcome to tutorialsandexamples") // Initializing s to "default string"
{
cout << "Default constructor invoked\n";
}
sample(const sample& var) : s(var.s) // Initializing s to obj.s
{
Cout << "Copy constructor invoked, Move failed!\n";
}
sample(sample&& var) : s(move(var.s)) // Transfer ownership using std::move
{
cout << "Move constructor invoked!\n";
}
// Method action returns string
string action()
{
return s;
}
};
// Global temp method of type sample (returns sample type object)
sample init(sample g)
{
return g;
}
//class G inherited from the sample class
Class G: public sample
{
};
int main()
{
// Create an object of class G using the default constructor
G var2;
// Move the object var2 to var3 using the move constructor
G var3 = move(var2);
return 0;
}
Output:
![Move Constructor in C++](https://static.tutorialandexample.com/cpp/move-constructor-in-cpp4.png)
The move constructor is used in a class inheritance scenario, as seen in the above C++ code. A derived class object is created, and using the move constructor, it is transferred to another object. The string member is effectively passed between objects because of the move constructor.
Example 3:
The example below demonstrates how the move constructor is not called if the inherited class declares a destructor.
Code:
#include <iostream>
#include <string>
using namespace std;
class sample {
private:
string s;
Public:
sample() : s("hello friends") // Initializing s to "hello friends"
{
cout << "Default constructor is called\n";
}
sample(const sample& var) : s(var.s) // Copying s from another object
{
Cout << "Copy constructor is invoked (Copy)\n";
}
sample(sample&& var) : s(move(var.s)) // Transferring ownership using std::move
{
Cout << "Move constructor is invoked (Move)\n";
}
string action()
{
return s;
}
};
// Global temp method of type sample (returns sample type object)
sample fun(sample t)
{
return t;
}
//class A inherited from the sample class
Class G: public sample
{};
//Class B inherited from the A class
class Y: public G
{
Public:
// This destructor prevents the implicit move constructor call
~Y() {};
};
int main()
{
// Below declaration calls the default constructor of the Y class
Y var1;
// Below declaration implicitly calls the move constructor of the sample class
Y var2 = move(var1);
return 0;
}
Output:
![Move Constructor in C++](https://static.tutorialandexample.com/cpp/move-constructor-in-cpp5.png)
Constructors, inheritance, and move semantics are illustrated in the above C++ code. It creates a sample class with copy, move, and default constructors. Additionally, it demonstrates inheritance between classes G and Y, where Y blocks the implicit call to the move constructor. Finally, new items are made and moved around.
Example 4:
The example below illustrates how the move constructor will be forced to be invoked if a default has been set in the inherited class's move constructor.
Code:
#include <iostream>
#include <string>
using namespace std;
class MyClass {
private:
string data;
Public:
MyClass(): data("Good morning!!") //Initializing data to "Good morning!!"
{
cout << "The Default constructor called\n";
}
MyClass(const MyClass& var) : data(var.data) // Initializing data by copying from another object
{
Cout << "Copy constructor invoked, Move failed!\n";
}
MyClass(MyClass&& var) : data(move(var.data)) // Transferring ownership using std::move
{
cout << "Move constructor invoked!\n";
}
// A method that returns the string
string action()
{
return data;
}
};
// Global temp function of type MyClass (returns MyClass type object)
MyClass hello(MyClass g)
{
return g;
}
// Class X inherited from the MyClass class
class X : public MyClass
{};
//class Y inherited from X class
class Y: public X
{
Public:
// This destructor prevents an implicit move constructor call
// ~Y() {};
};
class Z : public MyClass
{
Public:
//default constructor
Z() {}
//destructor would prevent an implicit call to the move constructor
~Z() {};
// Forced call to move constructor
Z(Z&&) = default;
};
int main()
{
// Create an object of class Z using the default constructor
Z fun1;
// Move the object fun1 to fun2 using the move constructor
Z fun2 = move(fun1);
return 0;
}
Output:
![Move Constructor in C++](https://static.tutorialandexample.com/cpp/move-constructor-in-cpp6.png)
The above code illustrates C++ move semantics. It establishes the default, copy, and move constructors for the class MyClass. Classes X, Y, and Z are included in the class hierarchy. The Z class overrides the destructor to stop an implicit call to the move constructor. A class Z object of type fun1 is created in the main() function using the default constructor, and then fun1 is transferred to fun2 using the move constructor.
Advantages:
- Performance gain: By transferring the resources rather than making copies, move constructors can drastically minimize the overhead associated with deep copying large objects. Move constructor is especially useful when working with objects that require a lot of memory allocation or sophisticated data structures.
- Resource Transfer: The move constructor makes it possible to efficiently transfer resources (such as dynamically allocated memory) from one object to another, avoiding the need for deep copying. By using the move constructor
- Lowering the cost of data copying is especially advantageous for objects with a lot of data.
- Reduced Memory Allocation and Deallocation: When moving objects, the move constructor can "steal" the resources directly from the source object, reducing needless memory allocation and deallocation. Move constructor can considerably increase the program's speed and memory effectiveness.
- Reduced Copying Overhead: The move constructor allows moving resource ownership without duplicating data, unlike the copy constructor, which generates a complete copy of an object. Move constructor shortens the processing and time needed to replicate large objects.
- Enhance Container Performance: Move semantics can be advantageous for containers like vector, string, and unique_ptr because it reduces the overhead of moving pieces within containers, especially during resizing or reallocation.
- Smart pointer efficiency: Move constructors are essential for effectively using smart pointers, such as unique_ptr, as they enable ownership transfer without the expense of deep copying.
- Performance Optimization for Function Returns: Move constructors can prevent temporary copies in functions returning large objects, resulting in speedier return values.
With the signature MyClass(MyClass&& other) (as mentioned in the above syntax), a move constructor in C++ facilitates effective resource transfer from one object to another of the same type. Utilizing rvalue references prevents needless copying and is especially helpful for managing large or expensive-to-copy objects. To move resources from the source object to the destination object easier, you utilize move() inside the move constructor.