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