I am studying the move constructor in C++ which receives a reference to an r-value of the class type.
The problem is that despite looking at several C++ Deitel books and several others, I find absolutely nothing about the move constructor , I only have information on a few slides that don't explain it clearly.
Is there any way you can help me to know how to find some information to learn the move constructor in C++?
Not even in Google do I find anything interesting to learn said constructor.
I'll try to be more specific, you see, it would be about the move constructor in C++11. That is to say something like:
class C
{
public:
C(C&& c) {...}
//...
}
I know that it is called implicitly when an object is initialized with a reference to r-value which is another object of the same class.
Then in the "little" of information I have, a vector class without object movement (with a copy constructor, a destructor and an assignment operator) is exposed.
And in the example I also get a movement constructor, movement assignment and little else, I don't know if this helps you to help me. Here I leave you with more information about the constructor that the example gives me:
Move Builder:
Vector(Vector&& v);
Definition:
//Ctor de movimiento
Vector::Vector(Vector&& v): eltos{v.eltos}, n_eltos{v.n_eltos}
{
v.eltos = nullptr; //Tras el movimiento se invoca
v.n_eltos = 0; // al destructor de v.
}
And another one of movement assignment:
Vector& Vector::operator= (Vector&& v) //Parametro no const
{
delete[] eltos;
eltos = v.eltos;
n_eltos = v.n_eltos;
v.eltos = nullptr; //Tras el movimiento se invocará
v.n_eltos = 0; //al destructor de v
return *this;
}
Previous references:
What is the move constructor ?
The move constructor was released with the C++11 standard. This constructor is part of what is known as the move syntax , which is roughly made up of the move constructor and a new assignment operator:
How does it work?
The
sintaxis move
goal is to avoid the unnecessary copying of large amounts of information. This is basically achieved by moving the pointers instead of moving the information contained in them:As you can see, calling the copy constructor involves allocating memory and copying data from one instance to another... whereas the move approach is dedicated to moving pointers around (much faster).
A side effect of the move syntax is that the original object is empty or unusable after the operation. Notice how the internal data of is reset
obj
in the move constructor . This is why the move syntax only makes sense when the original object is no longer needed.And what about the assignment operator?
The assignment operator is similar to the constructor but with a fundamental difference... we are going to modify an object that already exists instead of creating a new one and this forces us to take certain precautions such as cleaning the internal memory before dedicating ourselves to moving pointers:
How is the move syntax invoked ?
This is something that is already explained in the first link that I have put as a reference... I recommend reading it to understand the subject.
Is the move syntax the solution to all my problems?
Not joking. The move syntax is neither perfect nor can it always be applied... if you have objects that don't use dynamic memory you won't get any benefit. Furthermore, programming this syntax adds more code to the project... code that then has to be maintained and tested...
perfect forwarding
This would be the last detail that would be missing to comment on this topic since without it the move syntax is lame and can be a source of errors that are a bit complicated to understand.
You can find information about it in this other thread