* We use objects a Initialization object b, Post object a We're not using it
It's over , But the object a There's still room for you ( Before deconstruction ), Since the copy constructor , In fact, it's just a Copy the contents of the object to the b in , So why can't we use it directly a What about your space ? This avoids the allocation of new space , greatly
The cost of construction is reduced . This is the original intention of mobile constructor design ;
* Copy in constructor , For pointers , We must use deep replication , In the move constructor , For pointers , We use shallow replication . Shallow replication is dangerous , Because the two pointers point to a piece of memory
, If the first pointer releases it , The other pointer is illegal . So we just need to avoid the first pointer freeing up space
That's it . The way to avoid this is to put the first pointer ( such as a->value) Set as NULL, So when you call the destructor , Because there is a reason to judge whether it is NULL Statement of , So deconstruction a It's not going to be recycled when it's time a->value The space pointed to ;
* The parameters of the move constructor are different from those of the copy constructor , The argument to the copy constructor is an lvalue reference , But the initial value of the move constructor is a right value reference
. signify , The parameter of the move constructor is a reference to a right value or a dead value . in other words , Use only one right value , Or initialize the dead value to another object , Will call the move constructor . And that one move
sentence , That is to change a left value into a dead value .
Shallow replication  
#include<iostream> #include<vector> using namespace std; class Test { public:
Test(int num,char *ptr) { x = num; p = ptr; cout << " Constructors "<<endl; } Test(const
Test& a) :x(a.x),p(a.p) // Shallow replication { cout << " copy constructor " << endl; } Test(Test&& a) :
x(a.x) { p = a.p; a.p = NULL; cout << " move constructor " << endl; } ~Test() { if (p !=
NULL) delete p; } private: int x; char *p; }; int main() { int x = 8; char ch =
'A'; Test a(x,&ch); Test b(a); Test c(move(a)); return 0; }
output :

Constructors
copy constructor
move constructor

b.p and c.p Point to the same memory , Because it's all shallow replication . When there are pointer member variables , Copy constructor should be copied deeply ( Automatically generated copy constructors are shallow copies )

Deep replication
#include<iostream> #include<vector> using namespace std; class Test { public:
Test(int num,char *ptr) { x = num; p = ptr; cout << " Constructors "<<endl; } Test(const
Test& a) :x(a.x) { p = new char(1); // Deep replication *p = *(a.p); cout << " copy constructor " <<
endl; } Test(Test&& a) : x(a.x) { p = a.p; a.p = NULL; cout << " move constructor " <<
endl; } ~Test() { if (p != NULL) delete p; } int x; char *p; }; int main() {
int x = 2; char ch = '%'; char*p = &ch; cout << (void *)p << endl;
// The value of the output pointer should be void* Test a(x,p); Test b(a); Test c(move(a)); return 0; }

b Of p The value of the member is the same as the a Different ,c Of p Value and a.p identical

Technology
©2019-2020 Toolsou All rights reserved,
java Four functional interfaces ( a key , simple ) It's unexpected Python Cherry tree (turtle The gorgeous style of Library ) Browser kernel ( understand )HashMap Explain in detail Some East 14 Pay change 16 salary , Sincerity or routine ?html Writing about cherry trees , Writing about cherry trees