I am just starting to work in C ++ 11 mode, so I play with it. But I found something that I can’t understand.
#include <iostream>
using namespace std;
class A{
public:
A(){cout << "default ctor" << endl;}
A(const string& str):_str{str}{cout << "parameter ctor" << endl;}
A(A&& obj):_str{std::move(obj._str)}{cout << "move ctor" << endl;}
A& operator =(A&& rhs){_str = std::move(rhs._str);cout << "move assignment operation" << endl; return *this;}
void print(){cout << _str << endl;}
private:
string _str;
};
int main(){
A a("rupesh yadav");
A b(std::move(a));
cout << "print a: ";
a.print();
cout << "print b: ";
b.print();
b = std::move(a);
cout << "print a: ";
a.print();
cout << "print b: ";
b.print();
}
I expected that the operation b = std::move(a)will behave differently, because I apply the transition to object a a second time, but it copies the left object b to the right side object a , I don’t understand this part.
Or I did something wrong in programming. Please help if I do something wrong in the move operation.
EDIT: , undefined. , , a b, , b a?
, , ?