Move constructor without pointers

I know this question may seem very simple. But I just can't find an example of a move constructor with no pointers.

I have a class that contains a variable of a vector object. Not a pointer to one. So my question is: Does this mean that I don’t need in the move constructor? Or is my implementation incorrect and should I use a pointer to a vector and then use the move constructor?

thanks

+5
source share
3 answers

If your class contains movable objects (like a vector), then just let the compiler generate implicit move operations. They will do the right thing by moving each subobject.

You will only need to write your own if you are juggling raw pointers to dynamic objects or other unmanaged resource handles. The best approach is to have a properly copied / movable object (for example, a container, smart pointer, or other RAII class) to control each of them; then a complex object constructed by composing them will implicitly have the correct copy / move semantics.

(As noted in the comments: if your class declares its own destructor or copy operations, then it will not receive implicit move operations, you will need to define your own. But if you follow the advice above, these are from correctly managed types that do not need in any special processing, then you do not need to announce it.)

+10
source

Moving a constructor follows similar guidelines as copy constructors. This is necessary if your class contains elements with a pointer or other fragile data fields (sockets, db connections, etc.), so that the automatically generated one can damage the memory layout by deleting the same object twice. Therefore, if you do not have index fields, you do not need to write a move constructor.

+3
source

The default move constructor and move asignment generated by the compiler will call move constructor / asignment for each member of the class.

If your class has only raw elements, for example, "char * buffer", you should write your own move operations.

If your class has only “managed members,” such as “vector,” the default move operations for your class will be fine, as it delegates the operation to each member.

If your class has “managed members” mixed with “raw members”, vector and int *, for example, your move operations will have to manually move raw resources and invoke move operations for managed objects:

class MyObject { public: // ... MyObject(MyObject&& other) : vector1(std::move(other.vector1)) { // We use vector move constructor ^ // and then move ourself the char* buffer buffer1 = other.buffer1; other.buffer1 = nullptr; } MyObject& operator=(MyObject&& other) { // We use vector move asignment operator vector1= std::move(other.vector1); // and then move ourself the char* buffer std::swap(buffer1,other.buffer1); return *this; } //... private: vector<char> vector1; char * buffer1; }; 

std :: move (other.vector1) is required because inside that other.vector1 function is an lvalue. We must tell the compiler that we will not use the value of vector1 later in the functional code so that its value can be moved.

+1
source

Source: https://habr.com/ru/post/1202129/


All Articles