Move constructors and move assignment functions

Source: Internet
Author: User

Common copy constructors and moving copy constructors, general assignment functions and move assignment functions are implemented inside a class.
Call the regular function if the argument is an lvalue, or call the move function if the argument is an rvalue.
You can also call "Std::move" to forcibly invoke the move function.

#include  <iostream> #include  <utility>using std::cout;using std::endl;class  Useless{private:    int n;           // number of elements    char * pc;       // pointer to data    static int ct;   // number of objectspublic:    useless ();     Explicit useless (int k);     useless (int k, char ch);     useless (const useless & f);  // regular copy constructor     useless (useless && f);      //  Move constructor    ~useless ();     useless operator+ (const &NBSP;USELESS&NBSP;&AMP;&NBSP;F) const;     useless & operator= (const useless & f);  //  copy assignment    useless & operator= (Useless && &NBSP;F);       // move assignment     void  showobject ()  const;};/ / implementationint useless::ct = 0; Useless::useless () {    cout <<  "enter "  << __func__  <<  "() \ n";    ++ct;    n = 0;     pc = nullptr;    showobject ();    cout < <  "leave "  << __func__ <<  "() \ n";  }useless::useless (int  k)  : n (k) {    cout <<  "enter "  << __ func__ <<  "(k) \ n";    ++ct;     pc = new char[n];    showobject ();     cout <<  "leave "  << __func__ <<  " (k) \ n ";} Useless::useless (Int k, char ch)  : n (k) {    cout <<   "enter "  << __func__ <<  "(k, ch) \ n";     ++ ct;    pc = new char[n];    for  (int i =  0; i < n; i++)         pc[i] =  Ch;    showobject ();    cout <<  "leave "   << __func__ <<  "(k, ch) \ n";} Useless::useless (const useless & f):  n (F.N)  {    cout  <<  "enter "  << __func__ <<  "(const &) \ n";     ++ct;    pc = new  char[n];    for  (int i = 0; i < n; i++)         pc[i] = f.pc[i];    showobject () ;    cout <<  "leave "  << __func__ <<  "(const &) \ n";} Useless::useless (useless && f):  n (F.N)  {    cout <<   "enter "  << __func__ <<  "(&&) \ n";     ++ ct;    pc = f.pc;       // steal  address    f.pc = nullptr;  // give old object  Nothing in return    f.n = 0;    showobject ();     f.showobject ();    cout <<  "leave "   << __func__ <<  "(&&) \ n";} Useless::~useless () {    cout <<  "enter "  << __func__  <<  "() \ n";     showobject ();    --ct;     delete [] pc;    cout <<  "leave "  <<  __func__ <<  "() \ n";} Useless & useless::operator= (const useless & f)   // copy  assignment{    cout <<  "enter "  << __func__ < <  "(const &) \ n";     showobject ();     f.showobject ();     if  (this == &f)          Return *this;    delete [] pc;    n = f.n;    pc =  new char[n];    for  (int i = 0; i < n;  i++)         pc[i] = f.pc[i];     Showobject ();     f.showobject ();    cout <<  "leave   " << __func__ << " (const &) \ n ";     return  *this;} Useless & useless::operator= (useless && f)         // move assignment{    cout <<  "enter "  <<  __func__ <<  "(&&) \ n";     showobject ();     f.showobject ();    if  (this == &f)          return *this;    delete [] pc;    n = f.n;     pc = f.pc;    f.n = 0;     F.pc = nullptr;    showobject ();     f.showobject ();     cout <<  "leave "  << __func__ <<  "(& &) \ n ";     return *this;} useless useless::operator+ (const useless & f) const{    cout  <<  "enter "  << __func__ <<  "(const &) \ n";     showobject ();     f.showobject ();     useless temp  = useless (N&NBSP;+&NBSP;F.N);    for  (int i = 0; i  < n; i++)         temp.pc[i] = pc[i];    for  (int i = n; i <  temp.n; i++)         temp.pc[i] = f.pc[i - n];     cout <<  "\t temp: ";     temp. Showobject ();    cout <<  "leave "  << __func__  <<  "(const &) \ n";     return temp;} Void useless::showobject ()  const{     cout <<  "\t this = " << this << ",  ct= " << ct;      cout <<  ",  pc= ("  << n <<  ", "  <<   (void*) pc <<  ", ";     if  (n == 0)          cout <<  "(Object empty) ";    else        for  (int  i = 0; i < n; i++)              cout << pc[i];    cout << endl;}  applicationint main () {    useless one (10,  ' x ');     Useless two = one + one;   // calls move  constructor    cout <<  "object one:\n";     one. Showobject ();    cout <<  "object two:\n";     two. Showobject ();    useless three, four;    cout <<   "three = one\n";    three = one;               // automatic copy assignment    cout <<   "now object three:\n";     three. Showobject ();    cout <<  "and object one:\n";     one. Showobject ();    cout <<  "four = one + two\n";     four = one + two;         / / automatic move assignment    cout <<  "Now object  four:\n ";     four. Showobject ();    cout <<  "four = move (one) \ n";     four = std::move (one);    // forced move assignment     cout <<  "now object four:\n";     four. Showobject ();    cout <<  "and object one:\n";     one. Showobject ();      std::cin.get ();}

The test results are as follows. The red part is not part of the test result, it is an analysis of the test results.

Enter useless (k, ch)
THIS=0X7FFFB5ADE0F0, Ct=1, pc= (Ten, 0x23f7010, xxxxxxxxxx
Leave useless (k, ch)
Enter operator+ (const &)
THIS=0X7FFFB5ADE0F0, Ct=1, pc= (Ten, 0x23f7010, xxxxxxxxxx
THIS=0X7FFFB5ADE0F0, Ct=1, pc= (Ten, 0x23f7010, xxxxxxxxxx
Enter useless (k)
this=0x7fffb5ade100, ct=2, pc= (0x23f7030,
Leave useless (k)
temp:this=0x7fffb5ade100, ct=2, pc= (0x23f7030, xxxxxxxxxxxxxxxxxxxx
Leave operator+ (const &)
Object one:
THIS=0X7FFFB5ADE0F0, ct=2, pc= (Ten, 0x23f7010, xxxxxxxxxx
Object:
this=0x7fffb5ade100, ct=2, pc= (0x23f7030, xxxxxxxxxxxxxxxxxxxx
"Useless-one +one;"
First Call "operator+ (const &)" and call "useless (k)" Within this function to generate the Temp object.
Returns when the copy constructor is called to generate a temporary anonymous object.
The destructor Temp object.
The move copy constructor is then called to generate object A.
Destructors temporary anonymous objects.
Current GCC version is g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4, the compilation process has been improved, the Temp object and the object directly as an object, eliminating the four steps behind.
Enter useless ()
this=0x7fffb5ade110, ct=3, pc= (0, 0, (object empty)
Leave useless ()
Enter useless ()
this=0x7fffb5ade120, ct=4, pc= (0, 0, (object empty)
Leave useless ()
three = one
Enter operator= (const &)
this=0x7fffb5ade110, ct=4, pc= (0, 0, (object empty)
THIS=0X7FFFB5ADE0F0, ct=4, pc= (Ten, 0x23f7010, xxxxxxxxxx
this=0x7fffb5ade110, ct=4, pc= (Ten, 0x23f7050, xxxxxxxxxx
THIS=0X7FFFB5ADE0F0, ct=4, pc= (Ten, 0x23f7010, xxxxxxxxxx
Leave operator= (const &)
Now object three:
this=0x7fffb5ade110, ct=4, pc= (Ten, 0x23f7050, xxxxxxxxxx
and object one:
THIS=0X7FFFB5ADE0F0, ct=4, pc= (Ten, 0x23f7010, xxxxxxxxxx
four = one +
Enter operator+ (const &)
THIS=0X7FFFB5ADE0F0, ct=4, pc= (Ten, 0x23f7010, xxxxxxxxxx
this=0x7fffb5ade100, ct=4, pc= (0x23f7030, xxxxxxxxxxxxxxxxxxxx
Enter useless (k)
this=0x7fffb5ade130, Ct=5, pc= (0x23f7070,
Leave useless (k)
temp:this=0x7fffb5ade130, Ct=5, pc= (0x23f7070, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Leave operator+ (const &)
//"four = one +" First call the "operator+ (const &)" function. The Temp object is generated within this function.
After returning the "operator+ (const &)" function, a temporary anonymous object was not generated, and the Temp object was not refactored, but the move copy function "operator= (&&)" was called directly with the temp parameter.
Enter operator= (&&)
this=0x7fffb5ade120, ct=5, pc= (0, 0, (object empty)
this=0x7fffb5ade130, Ct=5, pc= (0x23f7070, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
this=0x7fffb5ade120, Ct=5, pc= (0x23f7070, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
this=0x7fffb5ade130, ct=5, pc= (0, 0, (object empty)
Leave operator= (&&)
//After returning the "operator= (&&)" function, the Temp object is not destructor.
Enter ~useless ()
this=0x7fffb5ade130, ct=5, pc= (0, 0, (object empty)
Leave ~useless ()
Now object four:
this=0x7fffb5ade120, ct=4, pc= (0x23f7070, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
four = Move (one)
//"four = Std::move (one);" Forcibly calls the move assignment function "operator= (&&)".
After the call, the four object takes over the internal resources of the one object (PC and N), the one object is not refactored, but the internal resources are "hollowed out"!
Enter operator= (&&)
this=0x7fffb5ade120, ct=4, pc= (0x23f7070, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
THIS=0X7FFFB5ADE0F0, ct=4, pc= (Ten, 0x23f7010, xxxxxxxxxx
this=0x7fffb5ade120, ct=4, pc= (Ten, 0x23f7010, xxxxxxxxxx
THIS=0X7FFFB5ADE0F0, ct=4, pc= (0, 0, (object empty)
Leave operator= (&&)
Now object four:
this=0x7fffb5ade120, ct=4, pc= (Ten, 0x23f7010, xxxxxxxxxx
and object one:
THIS=0X7FFFB5ADE0F0, ct=4, pc= (0, 0, (object empty)

The object that destructors the stack space when exiting "main ()". The destructor sequence is the opposite of the construction order.
Enter ~useless ()
this=0x7fffb5ade120, ct=4, pc= (Ten, 0x23f7010, xxxxxxxxxx
Leave ~useless ()
Enter ~useless ()
this=0x7fffb5ade110, Ct=3, pc= (Ten, 0x23f7050, xxxxxxxxxx
Leave ~useless ()
Enter ~useless ()
this=0x7fffb5ade100, ct=2, pc= (0x23f7030, xxxxxxxxxxxxxxxxxxxx
Leave ~useless ()
Enter ~useless ()
THIS=0X7FFFB5ADE0F0, Ct=1, pc= (0, 0, (object empty)
Leave ~useless ()


This article from the "write poetry with C + +" blog, declined reprint!

Move constructors and move assignment functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.