C + + Mobile Construction

Time:2022-4-30

The following essay gives the C + + mobile structure.

In reality, there are many such examples. We transfer money from one account to another and use mobile phonesSIMTransfer the card to another mobile phone and cut the file from one location to another……Mobile construction can reduce unnecessary replication and improve performance.

  • C++11The standard provides a new construction method – mobile construction.

  • C++11Previously, if you want to transfer the state of the source object to the target object, you can only copy it. In some cases, we don’t have to copy objects — we just need to move them.

  • C++11Introducing mobile semantics:

    • The control of the source object’s resources is handed over to the target object

  • move constructor

Problems and Solutions

  • When temporary objects are copied, they are no longer used. We can move the resources of temporary objects directly, so as to avoid redundant copy operations.

Mobile structure

  • When should a mobile construct be triggered?

    • There are temporary objects that can be used

  • move constructor :

    class_name ( class_name && )

1 // example: the function returns an object containing pointer members (version 1)
 2 
 3 // use the deep copy constructor
 4 
 5 // when returning, construct a temporary object, allocate dynamically, return the temporary object to the calling function, and then delete the temporary object.
 6 
 7 #include
 8 
 9 using namespace std;
10 
11 class IntNum {
12 
13 public:
14 
15 intnum (int x = 0): xptr (New int (x)) {// constructor
16 
17     cout << "Calling constructor..." << endl;
18 
19   }
20 
21 intnum (const intnum & n): xptr (New int (* n.xptr)) {// copy constructor
22 
23     cout << "Calling copy constructor..." << endl;
24 
25   };
26 
27 ~ intnum() {// destructor
28 
29     delete xptr;
30 
31     cout << "Destructing..." << endl;
32   
33   }
34 
35   int getInt() { return *xptr; }
36 
37 private:
38 
39   int *xptr;
40 
41 };
42 
43 // the returned value is an intnum object
44 
45   IntNum getNum() {
46 
47     IntNum a;
48 
49     return a;
50 
51   }
52 
53 int main() {
54 
55   cout<
1 // example: the function returns an object containing pointer members (version 2)
 2 
 3 // use the move constructor
 4 
 5 // transfer the local object to be returned to the calling function, eliminating the process of constructing and deleting temporary objects.
 6 
 7 #include
 8 
 9 using namespace std;
10 
11 class IntNum {
12 
13 public:
14 
15 intnum (int x = 0): xptr (New int (x)) {// constructor
16 
17     cout << "Calling constructor..." << endl;
18 
19   }
20 
21 intnum (const intnum & n): xptr (New int (* n.xptr)) {// copy constructor
22 
23     cout << "Calling copy constructor..." << endl;
24 
25 // Note:
26 
27 // • & & is an R-value reference
28 
29 //• the temporary variable returned by the function is the right value
30 
31   }
32 
33 intnum (intnum & & n): xptr (n.xptr) {// move constructor
34 
35    n.xptr = nullptr;
36   
37    cout << "Calling move constructor..." << endl;
38 
39   }
40 
41 ~ intnum() {// destructor
42 
43     delete xptr;
44 
45     cout << "Destructing..." << endl;
46 
47   }
48 
49 private:
50 
51   int *xptr;
52 
53 };
54 
55 // the returned value is the intnum class object
56 
57   IntNum getNum() {
58 
59   IntNum a;
60 
61   return a;
62 
63 }
64 
65 int main() {
66 
67   cout << getNum().getInt() << endl; return 0;
68 
69 }
70 
71 // running result:
72 
73 Calling constructor...
74 
75 Calling move constructor...
76 
77 Destructing...
78 
79 0
80 
81 Destructing...