It looks like a time-saver sometimes if I can call the move assignment op from the move ctor. But when I try, it takes me straight to conventional assignment:
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass() { }
MyClass(const MyClass& other) { /* do some stuff */ }
MyClass(MyClass&& other); //move ctor
const MyClass& operator= (const MyClass& other);
const MyClass& operator= (MyClass&& other); //move =
};
MyClass::MyClass(MyClass&& other) //move ctor
{
cout << "Calling move ctor\n";
*this = other; //<<--THIS IS THE PROBLEM
}
const MyClass& MyClass::operator= (MyClass&& other) //move =
{
cout << "Calling move =\n";
return *this;
}
const MyClass& MyClass::operator= (const MyClass& other)
{
cout << "Calling standard =\n";
if (this == &other) return *this;
return *this;
}
MyClass makeVectorToTestMoveCtor() { MyClass V; return V; }
int main ()
{
MyClass V = makeVectorToTestMoveCtor();
return 0;
}
I can force it with std::move:
cout << "Calling move ctor\n";
*this = std::move(other);
...but if it's not a bad idea, surely I shouldn't need to force it? What should I do here?