The Move Assignment Operator is one of the great features of Object-Oriented Programming in professional development. It complements features like the copy assignment operator, copy constructor, move constructor, and destructor. Since the C++11 standards, the move assignment operator is declared by using “operator=” and it allows you to move one object to another object. In this post, we explain what a move assignment operator is along with some C++ examples.
First, let’s remind ourselves what are the classes and objects in C++.
Table of Contents
What are classes and objects in C++?
Classes are defined in C++ using the keyword class followed by the name of the class. Classes are the blueprint for the objects and they are user-defined data types that we can use in our program. Objects are an instantiation of a class, In C++ programming, because it is designed to be strongly object oriented most of the commands are associated with classes and objects, along with their attributes and methods. Here is a simple class example below.
1 2 3 4 5 6 7 |
class Tx { public: std::string str; }; |
What is a move assignment operator in C++?
The Move Assignment Operator is an assignment operator that is a non-template non-static member function which is declared with the “operator=“. When you create a class or a type that is move assignable (that you can move objects with the std::move
), it must have a public move assignment operator. Here is a simple syntax for the typical declaration of a move assignment operator.
Syntax (Since C++11),
1 2 3 |
class_name & class_name ::operator=(class_name &&) |
Here is an example of a move assignment operator declaration in a class.
1 2 3 4 5 6 |
Tx& operator=(Tx&& other) { return *this; } |
This is how you can move one object to another one with move assignment operator.
1 2 3 4 5 |
Tx o1, o2; o2 = std::move(o1); |
When the move assignment operator is called, lvalue
object type of an assignment expression is the same type or implicitly converted type of the rvalue
object. Move assignment operator is similar to changing two pointers of data blocks in C language. These can be, pointers to data blocks (i.e bitmaps), pointers to any structs, pointers to dynamically-allocated objects, I/O streams, running threads, file descriptors, TCP sockets, etc.
Is there a simple example of using the move assignment operator in C++?
The move assignment operator is default in any class declarations. This means you don’t need to declare it as above, let’s give examples without using it.
Let’s give a simple C++ example to move assignment operator with default
option, here is a simple class.
1 2 3 4 5 6 7 8 |
class Tx { public: std::string str; }; |
Because this is default in any class declaration, and it is automatically declared. This class is same as below.
1 2 3 4 5 6 7 8 9 |
class Tx { public: std::string str; Tx& operator=(Tx&& other) = default; // Move Assignment Operator }; |
And here is how you can use move assignment operator with both class examples above.
1 2 3 4 5 |
Tx o1, o2; o2 = std::move(o1); |
Is there a full example of how to use the move assignment operator in C++?
Here is an example with a move assignment operator in a class that moves one object to another.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <iostream> #include <string> class Tx { public: std::string str ; Tx& operator=(Tx&& other) // move assignment operator { str = std::move(other.str); } }; int main() { Tx o1, o2; o1.str = "LearnCplusplus.org"; o2 = std::move(o1); // Using Move Assignment Operator std::cout << o1.str << std::endl; std::cout << o2.str << std::endl; system("pause"); return 0; } |
Here is the output.
1 2 3 4 |
LearnCplusplus.org Press any key to continue . . . |
As you see, in Modern C++, we can specialize the “std::move
” on what to move or not, with the move assignment operator.
Is there a move assignment operator in a simple class?
Note that, a simple empty C++ class is perfectly equivalent to default implementations (Rule of Five) in a class. A modern compiler is able to provide all these special member functions (default implementations). For example, see this simple class below:
1 2 3 4 5 6 |
class Tx { }; |
is exactly the same as the one below in modern C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Tx { public: Tx() = default; // constructor Tx(Tx const& other) = default; // copy constructor Tx& operator=(Tx const& other) = default; // copy assignment operator Tx(Tx&& other) = default; // move constructor Tx& operator=(Tx&& other) = default; // move assignment operator ~Tx() = default; // destructor }; |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition