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.
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),
Here is an example of a move assignment operator declaration in a class.
This is how you can move one object to another one with move assignment operator.
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.
Because this is default in any class declaration, and it is automatically declared. This class is same as below.
And here is how you can use move assignment operator with both class examples above.
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.
Here is the output.
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:
is exactly the same as the one below in modern C++.
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.