C++11 move semantics

Source: Internet
Author: User

C++11 move semantics

#define_crt_secure_no_warnings#include<iostream>#include<string>#include<vector>#include<map>//C + + also has a widely agreed to the argument that the address can be taken, the name is the left value, conversely, can not take the address, no name is the right value. //The rvalue, relative to the Lvalue, represents a literal constant, an expression, a non-reference return value for a function, and so on. /*rvalue references are used to support the transfer semantics. Transfer semantics can move resources (heaps, system objects, and so on) from one object to another, reducing the creation, copying, and destruction of unnecessary temporary objects, which can significantly improve the performance of C + + applications. Maintenance (creation and destruction) of temporary objects has a serious impact on performance. The transfer semantics are relative to the copy semantics and can be analogous to the cut and copy of a file, and when we copy a file from one directory to another, the speed is much slower than the cut. By transferring semantics, the resources in the temporary object can be transferred to other objects. *//*definition of moving semantics: in the existing C + + mechanism, we can define copy constructors and assignment functions. To implement transfer semantics, you need to define a transfer constructor, and you can also define a transfer assignment operator. The transfer constructor and transfer assignment operators are called for copy and assignment of the right value. If the transfer constructor and the transfer copy operator are not defined, then the existing mechanism is followed, and the copy constructor and assignment operator are called. Normal functions and operators can also use rvalue reference operators to implement the transfer semantics. */classmystring{ Public: MyString (Const Char*tmp ="ABC")    {//Common ConstructorsLen = strlen (TMP);//lengthstr =New Char[len+1];//Heap Area application spacestrcpy (STR, TMP);//Copy Contentcout<<"Common constructor str ="<< Str <<Endl; } MyString (ConstMyString &tmp) {//copy ConstructorLen =Tmp.len; STR=New Char[Len +1];        strcpy (str, TMP.STR); cout<<"copy constructor tmp.str ="<< Tmp.str <<Endl; }    //Move Constructor//parameter is a non-const rvalue referenceMyString (MyString &&t) {str= T.str;//copy Address, no re-application memoryLen =T.len; //Original pointer emptyT.str =NULL; cout<<"Move Constructor"<<Endl; } MyString&operator= (ConstMyString &tmp) {//assignment operator overloaded functions        if(&tmp = = This)        {            return* This; }        //release the original memory firstLen =0; Delete[]str; //re-request contentLen =Tmp.len; STR=New Char[Len +1];        strcpy (str, TMP.STR); cout<<"assignment operator overloaded function tmp.str ="<< Tmp.str <<Endl; return* This; }    //Move Assignment Function//parameter is a non-const rvalue referenceMyString &operator= (MyString &&tmp) {        if(&tmp = = This)        {            return* This; }        //release the original memory firstLen =0; Delete[]str; //no need to re-apply heap area spaceLen =Tmp.len; STR= Tmp.str;//Address Assignment ValueTmp.str =NULL; cout<<"move assignment function \ n"; return* This; }    ~MyString () {// Destructorscout <<"Destructors :"; if(str! =NULL) {cout<<"operation Delete, str ="<<str; Delete[]str; STR=NULL; Len=0; } cout<<Endl; }Private:    Char*str =NULL; intLen =0;}; MyString func ()//returns a normal object, not a reference{MyString obj ("Mike"); returnobj;}voidmytest () {MyString&AMP;&AMP;TMP1 = func ();//rvalue Reference ReceiveMyString TMP2 ("ABC");//instantiate an objectTMP2 =func (); return;}intMain () {mytest (); System ("Pause"); return 0;}

C++11 move semantics

Related Article

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.