I would like to disable the move constructor in the class. Instead of moving, I would like to base on copy constructor. When I try to write this code:
class Boo
{
public:
Boo(){}
Boo(const Boo& boo) {};
Boo(Boo&& boo) = delete;
};
Boo TakeBoo()
{
Boo b;
return b;
}
during compilation I received error:
error C2280: 'Boo::Boo(Boo &&)': attempting to reference a deleted function
How can I disable the move constructor and force copies instead?
To correct this, remove the move constructor completely. In the case of the class, once a copy constructor is present (user defined), the move is implicitly not generated anyway (move constructor and move assignment operator).
No move constructor is automatically generated.
A move constructor allows the resources owned by an rvalue object to be moved into an lvalue without creating its copy. An rvalue is an expression that does not have any memory address, and an lvalue is an expression with a memory address.
For non-union class types (class and struct), the move constructor performs full member-wise move of the object's bases and non-static members, in their initialization order, using direct initialization with an xvalue argument.
Do not create any move constructor:
class Boo
{
public:
Boo(){}
Boo(const Boo& boo) {};
};
The move constructor is not automatically generated as long as a user-defined copy constructor is present so the copy constructor is called.
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