I read in another question that when implementing a move constructor it is good practice to std::move each member in the initializer list because if the member happens to be another object then that objects move constructor will be called. Like so...
//Move constructor
Car::Car(Car && obj)
:
prBufferLength(std::move(obj.prBufferLength)),
prBuffer(std::move(obj.prBuffer))
{
obj.prBuffer = nullptr;
obj.prBufferLength = 0;
}
However in all the sample move assignment operators I've seen, there has been no mention of using std::move for the same reasons. If the member is an object then should std::move be used? Like so...
//Move assignment
Car Car::operator=(Car && obj)
{
delete[] prBuffer;
prBufferLength = std::move(obj.prBufferLength);
prBuffer = std::move(obj.prBuffer);
obj.prBuffer = nullptr;
obj.prBufferLength = 0;
return *this;
}
UPDATE:
I appreciate there is no need to use std::move in the example I have chosen (poorly) however I'm interested in if the members were objects.
After reading the linked question, I can see the advice in the second most-upvoted answer is to use std::move
in the initializer list for the move constructor because no matter if it is a primitive type or not, it will do the right thing. I somewhat disagree with that and think you should only call std::move
where appropriate, but this is were personal preferences come in.
Also, for your move assignment operator, the way you have it is fine although I think the unnecessary call to std::move
should be removed personally. Another option is to use std::swap
which will do the right thing for you.
Car Car::operator=(Car && obj)
{
std::swap(this->prBufferLength, obj.prBufferLength);
std::swap(this->prBuffer, obj.prBuffer);
return *this;
}
The difference between the above move assignment operator and your move assignment operator is that the deallocation of memory is delayed while your version deallocates the memory right away, this might be important in some situations.
It looks like prBuffer
is a pointer and prBufferLength
is some kind of integral type, so move
isn't going to make any difference in this particular case as they are both fundamental types.
If prBuffer
was a std::string
for example, then you should use move
to force the use of a move constructor or move assignment operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With