Move Assignment Operator in C++ 11 Last Updated : 26 Aug, 2025 Comments Improve Suggest changes 2 Likes Like Report Move assignment is a way to transfer resources (like memory, files, etc) from one object to another without copying them.Example: C++ #include <iostream> #include <vector> // for std::move #include <utility> int main() { std::vector<int> a = {1, 2, 3, 4}; std::vector<int> b; // move assignment! b = std::move(a); // a is now empty std::cout << "a.size() = " << a.size() << "\n"; // b has the data std::cout << "b.size() = " << b.size() << "\n"; } Outputa.size() = 0 b.size() = 4 Move Assignment OperatorIt is a special function that lets an object take ownership of resources from another object without copying.User-Defined Move Assignment OperatorThe programmer can define the move assignment operator .Example: C++ #include <iostream> #include <cstring> class MyString { char* data; public: // Constructor MyString(const char* str = "") { data = new char[strlen(str) + 1]; strcpy(data, str); } // User-defined move assignment operator MyString& operator=(MyString&& other) { std::cout << "Move assignment called\n"; if (this != &other) { // Free old memory delete[] data; // Steal the pointer data = other.data; // Set source to null other.data = nullptr; } return *this; } // Destructor ~MyString() { delete[] data; } void print() const { if (data) std::cout << data << "\n"; else std::cout << "[empty]\n"; } }; int main() { MyString a("Hello"); MyString b("World"); b = std::move(a); b.print(); a.print(); } OutputMove assignment called Hello [empty] Need of Move Assignment OperatorImproves performance - Move assignment transfers resource instead of copying them, which is much faster, especially for large objects.Reduces memory usage - It avoids creating unnecessary duplicates in memory by reusing existing resources.Handles temporary objects efficiently - Move assignment allows objects to take ownership of data from temporary values without expensive copying. Create Quiz Comment B beliver01 Follow 2 Improve B beliver01 Follow 2 Improve Article Tags : C++ Geeks Premier League C++ 11 Geeks Premier League 2023 Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like