Adding a move constructor and a move assignment operator in a string

Source: Internet
Author: User

13.50 No destructor defined

#include <iostream>#include<string>#include<memory>#include<utility>#include<cstring>#include<vector>using namespacestd;classstring{ Public: String (): Elements (nullptr), First_free (nullptr) {}String (Char*c); String (Conststring&); String&operator=(Conststring&); string* BEGIN ()Const{returnelements;} string* END ()Const{returnFirst_free;} String (String&&); String&operator= (String &&);Private:    Staticallocator<string>Alloc; string*elements; string*First_free;}; Allocator<string>String::alloc; String::string (Char*c) {size_t capacity=strlen (c); Auto Data=alloc.allocate (capacity); Auto Dest=data; strings;    S.copy (C,strlen (c)); Alloc.construct (dest++, s); Elements=data; First_free=dest;} String::string (ConstString &s) {cout<<"Copy Construct"<<Endl; Auto Capacity=s.end ()-S.begin (); Auto Data=alloc.allocate (capacity);    Uninitialized_copy (S.begin (), S.end (), data); Elements=data; First_free=data+capacity;} String& String::operator=(ConstString &s) {cout<<"copy = construct"<<Endl; Auto Capacity=s.end ()-S.begin (); Auto Data=alloc.allocate (capacity);    Uninitialized_copy (S.begin (), S.end (), data); if(elements) {Auto begin=elements; Auto End=First_free;  while(begin!=end) Alloc.destroy (Begin++); Alloc.deallocate (Elements,first_free-elements); } Elements=data; First_free=data+capacity; return* This;} String::string (String&&s): Elements (s.elements), First_free (s.first_free) {cout<<"Move Construct"<<Endl; S.elements=s.first_free=nullptr;} String& String::operator= (String &&s) {cout<<"move = construct"<<Endl; if( This!=&s) {if(elements) {Auto begin=elements; Auto End=First_free;  while(begin!=end) Alloc.destroy (Begin++); Alloc.deallocate (Elements,first_free-elements); }} elements=s.elements; First_free=S.first_free; S.elements=s.first_free=nullptr; return* This;}intmain () {vector<String>VEC; Charch[]="Hello"; Charch1[]="world!"; cout<<vec.capacity () <<Endl; cout<<Endl;    Vec.push_back (String (ch)); cout<<vec.capacity () <<Endl; cout<<Endl;    Vec.push_back (String (CH1)); cout<<vec.capacity () <<Endl; cout<<Endl;    Vec.push_back (String (ch)); cout<<vec.capacity () <<Endl; cout<<Endl;    Vec.push_back (String (CH1)); cout<<vec.capacity () <<Endl; cout<<Endl;    Vec.push_back (String (CH1)); cout<<vec.capacity () <<Endl; cout<<Endl;    Vec.push_back (String (CH1)); cout<<vec.capacity () <<Endl; return 0;}

The result is as follows: The move constructor appears in the result because the result of calling the string constructor is the right value

When defining destructors:

#include <iostream>#include<string>#include<memory>#include<utility>#include<cstring>#include<vector>using namespacestd;classstring{ Public: String (): Elements (nullptr), First_free (nullptr) {} String (Char*c); String (Conststring&); String&operator=(Conststring&); string* BEGIN ()Const{returnelements;} string* END ()Const{returnFirst_free;} //Be sure to define the destructor, or you will not call the copy constructor if you define the move constructor~String () {if(elements) {Auto begin=elements; Auto End=First_free;  while(begin!=end) Alloc.destroy (Begin++); Alloc.deallocate (Elements,first_free-elements); }} string (String&&) noexcept; String&operator= (String &&) noexcept;Private:    Staticallocator<string>Alloc; string*elements; string*First_free;}; Allocator<string>String::alloc; String::string (Char*c) {size_t capacity=strlen (c); Auto Data=alloc.allocate (capacity); Auto Dest=data; strings;    S.copy (C,strlen (c)); Alloc.construct (dest++, s); Elements=data; First_free=dest;} String::string (ConstString &s) {cout<<"Copy Construct"<<Endl; Auto Capacity=s.end ()-S.begin (); Auto Data=alloc.allocate (capacity);    Uninitialized_copy (S.begin (), S.end (), data); Elements=data; First_free=data+capacity;} String& String::operator=(ConstString &s) {cout<<"copy = construct"<<Endl; Auto Capacity=s.end ()-S.begin (); Auto Data=alloc.allocate (capacity);    Uninitialized_copy (S.begin (), S.end (), data); if(elements) {Auto begin=elements; Auto End=First_free;  while(begin!=end) Alloc.destroy (Begin++); Alloc.deallocate (Elements,first_free-elements); } Elements=data; First_free=data+capacity; return* This;} String::string (String&&s) noexcept:elements (s.elements), First_free (s.first_free) {cout<<"Move Construct"<<Endl; S.elements=s.first_free=nullptr;} String& String::operator= (String &&s) noexcept{cout<<"move = construct"<<Endl; if( This!=&s) {if(elements) {Auto begin=elements; Auto End=First_free;  while(begin!=end) Alloc.destroy (Begin++); Alloc.deallocate (Elements,first_free-elements); }} elements=s.elements; First_free=S.first_free; S.elements=s.first_free=nullptr; return* This;}intmain () {vector<String>VEC; Charch[]="Hello"; Charch1[]="world!"; cout<<vec.capacity () <<Endl; cout<<Endl;    String SS (CH);    Vec.push_back (ss); cout<<vec.capacity () <<Endl; cout<<Endl;    Vec.push_back (String (CH1)); cout<<vec.capacity () <<Endl; cout<<Endl;    Vec.push_back (String (ch)); cout<<vec.capacity () <<Endl; cout<<Endl;    Vec.push_back (String (CH1)); cout<<vec.capacity () <<Endl; cout<<Endl;    Vec.push_back (String (CH1)); cout<<vec.capacity () <<Endl; cout<<Endl;    Vec.push_back (String (CH1)); cout<<vec.capacity () <<Endl; cout<<"\ n"; Std::vector<String>v;    String s;  for(Unsigned i =0; I! =4; ++i) {std::cout<< v.capacity () <<"\ n";    V.push_back (s); }    return 0;}

The results of the operation are as follows:

Adding a move constructor and a move assignment operator in a string

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.