How to Define a Move Constructor in C++? Last Updated : 01 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, we have a move constructor which is a part of C++11 move semantics and is used to handle resources for temporary and rvalue objects. In this article, we will learn how to write a move constructor in C++. How to Write Move Constructor in C++?The move constructor is defined similarly to a copy constructor, but it takes an rvalue reference (Type&&) to the object's class type as its parameter. To define a move constructor in C++ use the below syntax: Syntax to Define Move Constructor in C++className(className&& source) noexcept;Here, className is the name of our class.&& indicates an rvalue reference to classname and source is the source object from which resources are being moved.noexcept specifier is used to indicate that the move constructor does not throw exceptions.C++ Program to Define Move ConstructorThe below example shows how can we write a move constructor and use it in C++. C++ // C++ program to declare move constructor #include <iostream> #include <vector> using namespace std; // Move Class class moveClass { private: // Declaring the raw pointer int* p; public: // Default Constructor moveClass(int d) { // Declaring object in the heap p = new int; *p = d; cout << "Default Constructor is called for " << d << endl; }; // Move Constructor moveClass(moveClass&& source) : p{ source.p } { cout << "Move Constructor is called for " << *source.p << endl; source.p = nullptr; } // Destructor ~moveClass() { cout << "Destructor invoked!\n"; // Free up the memory assigned to // The data member of the object delete p; } }; int main() { // Vector of Move Class vector<moveClass> vec; // Inserting Object of Move Class vec.push_back(moveClass{ 10 }); return 0; } OutputDefault Constructor is called for 10 Move Constructor is called for 10 Destructor invoked! Destructor invoked! Comment More infoAdvertise with us Next Article How to Define a Move Constructor in C++? V vikas2gqb5 Follow Improve Article Tags : C++ Programs C++ cpp-class cpp-constructor C++-Constructors C++ 11 CPP-OOPs CPP Examples +4 More Practice Tags : CPP Similar Reads How to Define the Default Constructor in C++? In C++, a constructor that takes no parameters is called a default constructor. A default constructor gets automatically invoked when an object of a class is created without any arguments. It is mainly used to initialize legal initial values to the member variables or perform other setup tasks. In t 2 min read Move Constructors in C++ A move constructor is a special type of constructor in C++ that is used to create a new object from the already existing object of the same type, but instead of making a copy of it, it makes the new object point to the already existing object in the memory, leaving the source object in a valid but u 10 min read How to Declare a Copy Constructor in a Derived Class? In C++, a copy constructor is a constructor that initializes an object as a copy of an existing object of the same class. In this article, we will learn how to declare a copy constructor in a derived class. Declaring a Copy Constructor in a Derived ClassWe can declare a copy constructor in a derived 3 min read How to Implement a Copy Constructor in a Derived Class in C++ In object-oriented programming, a copy constructor is a special member function that initializes a new object as a copy of an existing object. In this article, we will learn how to implement a copy constructor in a derived class. Implementing Copy Constructor in a Derived Class in C++ In C++, when w 3 min read How to Implement a Copy Constructor for Deep Copying? In C++, the copy constructor enables us to initialize an object using another object of the same class. In this article, we will learn, how to implement a copy constructor for deep copying. Implementing a Copy Constructor for Deep CopyingIn C++, a copy constructor is used to create a copy of an obje 3 min read Like