If the second object is a temporary object that is destroyed after the copy or assignment ends, then the move constructor and the move assignment operator are called, and the benefit is to avoid deep copying and increase efficiency .
To visually observe the operation of the move constructor and the move assignment operator, write a class pointer object that dynamically creates an array on the heap.
The code is as follows:
Class Pointer{public:pointer (const int I,const string &n) {mptr = new Int[i]; length = I;name = N;cout << "with parametric constructor Number \ "; Showid (); }pointer (): Mptr (nullptr), Length (0) {cout << "parameterless constructor \ n"; Showid ();} Virtual ~pointer () {cout <<name+ "destructor \ n"; if (mptr) delete[] mptr;mptr = nullptr;} Pointer (const pointer& s) {length = S.getlen (); mptr = new Int[length];name = s.name;cout << "copy constructor \ n"; Showid (); }pointer& operator= (const Pointer &s) {if (this = = &s) return *this;if (mptr) delete[] mptr;length = S.getlen (); Mptr = new Int[length];name = s.name;cout << "assignment operator \ n"; Showid (); return *this;} To move the constructor, the parameter s cannot be const pointer&& s, because the value of member data to change S Pointer (pointer&& s) {length = S.getlen (); mptr = S.getmptr (); name = S.name + "_yidonggouzao";//When you call move constructor, add a token s.mptr = nullptr;cout << move constructor \ n; showid ();} Move assignment operator pointer& operator= (pointer&& s) {if (this = = &s) return *this;if (mptr) delete[] Mptr;length = S.getlen (); mptr = S.mptr;name = S.NAme+ "_yidongfuzhi";//When you call the Move assignment operator, add a tag s.mptr = nullptr;cout << move assignment operator \ n; showid (); return *this;} void Showid () {cout << "length:" << length<< "pointer:" << mptr << "First Name:" <<name<< Endl;} int Getlen () Const{return length;} int* getmptr () Const{return mptr;} private:int* mptr;int length;string name= "#NULL";//This parameter is used to mark different objects, and c++11 supports initialization directly at the data member definition of the class}; Pointer Test () {Pointer A (2, "test"); return A;} int _tmain (int argc, _tchar* argv[]) {//fills brackets are to observe the call of the destructor {Pointer (4, "notname1");//After this statement is finished, the variable is immediately destroyed, so this is a temporary variable Pointer A1=test ();//Call Move constructor, create object A1cout << "A1.showid (): \ n"; A1.showid (); Pointer A2;a2=pointer (5, "notname2");//Call the Move assignment operator Pointer A3 (Pointer (7, "Notname3"));//The move constructor is not called here, <span style= " Font-family:arial, Helvetica, Sans-serif; >pointer (7, "Notname3") this variable is not immediately destroyed (that is, not a temporary variable) </span>, perhaps because it has a name A3, so <span style= "font-family:arial, Helvetica, Sans-serif; " > is not a temporary variable </span>cout << "A3.showid (): \ n"; A3.showid ();//Verify A3 is pointer (7, "Notname3")}cout << endl;system ("pause"); return 0;}
The results of the operation are as follows:
Move constructors and move assignment operators