c++ - Double move on same object is copying from left to right? -


i beginner in move operation in c++11, playing it. found not able 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("rupesh yadav"); // parameter ctor     b(std::move(a));   // move ctor      cout << "print a: ";     a.print();           // not printing  --> correct!!     cout << "print b: ";     b.print();           // printing      --> correct!!      b = std::move(a);    // don't know may silly still lets why not!!!, mistake??      cout << "print a: ";      a.print();           // printing      --> wrong!!      cout << "print b: ";      b.print();           // not printing  --> wrong!! } 

i expecting b = std::move(a) operation behave different because applying move on object a second time copying left side object b right hand side object a, part don't understand.

or have done wrong in programming. please if doing wrong in move operation.

edit: know undefined behavior. doubt if again copying object a object b, , if again same thing copy object b object a?

hence copying form left right , right left why?

you can't move same object twice.

after first moved a b, a had "valid unspecified state" (can't remember exact terminology). then, attempted move a b again! heck has broken loose. (i suspect that, internally, data pointers have been swapped around.)

simply don't this. see no reason want to.


Comments

Popular posts from this blog

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -