Considering the following snippet :
class Foo
{
public:
/* ... */
Foo(Foo &&other);
~Foo();
Foo &operator=(Foo &&rhs);
private:
int *data;
};
Foo::Foo(Foo &&other)
{
data = other.data;
other.data = nullptr;
}
Foo::~Foo()
{
delete data;
}
Foo &Foo::operator=(Foo &&other)
{
if (this == &other) return *this;
delete data; /* SAME AS DESTRUCTOR */
data = other.data; /* SAME AS MOVE CONSTRUCTOR */
other.data = nullptr;
return *this;
}
This snippet is pretty much what I always end up having.
Why do we need a move operator if its behavior can be deduced ?
If this statement isn't true, in which case the move operator behave differently than just destructor + move constructor ?