Site icon Learn C++

How To Learn The Move Constructors In Modern C++?

How To Learn The Move Constructors In Modern C++

Object Oriented Programming in C++ is greatly strengthened by the new standards of modern C++. One of the features of modern C++ is the move constructor that allows you to move the resources from one object to another object without copying them. In this post, we list the move constructor types that we are using in modern C++.

What is a constructor in C++?

The Constructor in C++ is a function, a method in the class, but it is a ‘special method’ that is automatically called when an object of a class is created. We don’t need to call this function. Whenever a new object of a class is created, the Constructor allows the class to initialize member variables or allocate storage. This is why the name Constructor is given to this special method. Here is a simple constructor class example below,

[crayon-67412cfc90337985208245/]

What is a move constructor in C++?

There are different constructor types in C++ Classes and the Move Constructor is one of these. Move Constructors not only used in classes but also used with struct and union data types. The Move Constructor is a constructor that allows you to move the resources from one object to another object without copying them. In other words, the move constructor allows you to move the resources from an rvalue object into to an lvalue object.

The Move Constructor of class T is a non-template constructor whose first parameter is class_name&&const class_name&&, volatile class_name&&, or const volatile class_name&&. It can be used with no other parameters or with the rest of the parameters all have default values.

This general syntax is also a syntax for the “Typical declaration of a move constructor” as in below,

[crayon-67412cfc9033f465643883/]

Syntax to use Move Constructor,

[crayon-67412cfc90341389214105/]

For more details about this please see this below,

Object Oriented Programming in C++ is highly evolved by the new standards of modern C++. One of the features of modern C++ is the move constructor that allows you to move the resources from one object to another object without copying them. In this post, we list the move constructor types in modern C++

What are the move constructors used in C++?

Now let’s see move constructors used in C++,

Typical declaration of a move constructor

The typical declaration of a move constructor is a move constructor declaration method that has user defined declaration and definition parts, and this is how you can declare typical move constructor in a class.

An example with a class;

[crayon-67412cfc90342484292318/]

We can use move constructor with std::move as in example below.

[crayon-67412cfc90343364905857/]

For more details about this please see this below,

Default (forced) move constructor in modern C++

The default (forced) move constructor is a move constructor deceleration method that has forced by = default option. This default option is forcing a move constructor to be generated by the compiler, here is how you can do forcing move constructor in a class.

[crayon-67412cfc90349203368206/]

For more details about this feature please see this post below,

Trivial move constructor

The Trivial Move Constructor is a Move Constructor which is implicitly defined or defaulted and has no virtual member functions, no base classes. The trivial move constructor generally a constructor that comes from template class or base class. The move constructor selected for every direct base of T or for every non-static class type (including array of class type) of T is trivial move constructor.

In general, the trivial move constructor is a constructor that makes a copy of the object representation by using std::memmove, it operates like the trivial copy constructor. Note that, all POD data types (data types compatible with the C language) are trivially movable.

In modern C++ example, this simple Tx class has a move constructor.

[crayon-67412cfc9034c300762718/]

Now, we can define a new Ty class and we can use Tx class as a base class as below.

[crayon-67412cfc9034d572178387/]

As you see, this new Ty class above has a trivial move constructor from Tx class. Because it is implicitly defined or defaulted.

For more details about this feature please see this post below,

Eligible move constructor

Since C++11, the Eligible Move Constructor is a Move Constructor which is eligible if it is not deleted. This definition is changed after C++20, the Eligible Move Constructor is a Move Constructor which is eligible if it is not deleted, if it has any associated constraints that are satisfied, if it has no move constructor with the same first parameter type is more constrained.

Until C++20, the move constructor is eligible:

Since C++20, the move constructor is eligible:

A simple class has an eligible move constructor because it has a default move constructor that compiler automatically defines, in Modern C++ it is similar as below.

[crayon-67412cfc90350017705227/]

Now, we can define a new Ty class and we can use one of Tx classes above as a base class here.

[crayon-67412cfc90352757429651/]

Deleted move constructor

Assume that there is a Tx class example with a declared and defined move constructor that uses std::move. We can delete this move constructor as below.

[crayon-67412cfc90355044697363/]

Implicitly deleted move constructor

Now, we can define a new Ty class and we can use Tx class as a base class as below.

[crayon-67412cfc90356300877347/]

Deleted Implicitly-declared Move Constructor

As you can see deleted move constructors above, this Ty class above has the implicitly-deleted move constructor from Tx class. We cannot use a move constructor with std::move as in example below:

[crayon-67412cfc90357610917793/]

Here o2 can not be declared by std::move because Ty has Tx class which has deleted move constructor.

For more details about this feature please see this post below,

Implicitly-declared move constructor

The implicitly-declared move constructor in modern C++ is a move constructor that is declared implicitly by using the move constructor of another base class. In other terms you have a new class that uses the base class, this class has implicitly declared a move constructor from the base class.

Let’s give a simple C++ example of an implicitly-declared move constructor which is a move constructor of other base class. Let’s assume that we have Tx as a base class and we have a new Ty class. This new class can use the move constructor from the Tx. Here is a Tx class example with a declared and defined move constructor that uses std::move,

[crayon-67412cfc9035a041628496/]

As given here above, if you have a move constructor, you should define a Constructor too, otherwise you will have “No matching constructor for initialization of class” error in compilation. Now, we can define a new Ty class and we can use Tx class as a base class as below.

[crayon-67412cfc9035b606810783/]

For more details about this feature please see this post below,

Implicitly-defined move constructor

The Implicitly-defined Move Constructor is a Move Constructor which is implicitly defined by another base, or it is an implicitly-declared move constructor neither deleted nor trivial.

We can define our own move constructor as shown here:

[crayon-67412cfc9035c144762159/]

And, we can use this Tx class as base class to define a new Ty class as below.

[crayon-67412cfc90368346021408/]

For more details about this feature please see this post below,

Note that this struct can be used in C applications too. As you see, in modern C++, we can easily read WAV wave files, now you can display or edit wave files.

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.

Exit mobile version