In reality there are many such examples where we transfer money from one account to another, moving the phone SIM card to another phone, cut the file from one location to another ... moving constructs can reduce unnecessary duplication, resulting in a performance boost.
L A new construction method-Mobile construction-is provided in the C++11 standard.
L Before c++11, if you want to transfer the state of the source object to the target object only by copying. In some cases, we don't need to copy objects-just move them.
L C++11 Introduces moving semantics:
n control of source object resources are all given to the target object
L Move Constructor
Problems and Solutions
When the temporary object is copied, it is no longer being exploited. We can completely move the resources of the temporary object directly, thus avoiding redundant copying operations.
Mobile Construction
When should I trigger a move construct?
n have temporary objects that can be exploited
L Move the constructor:
Class_name (class_name &&)
Example: A function returns an object containing a pointer member (version 1)
l use deep copy constructors
On return, constructs a temporary object, dynamically assigns the temporary object back to the keynote function, and then deletes the temporary object.
#include <iostream>using namespace Std;class intnum {public: intnum (int x = 0): xptr (new int (x)) {//constructor Co UT << "Calling constructor ..." << Endl; } Intnum (const Intnum & N): xptr (new int (*n.xptr)) {//copy constructor cout << "calling copy constructor ..." << en DL; }; ~intnum () {//destructor delete xptr; cout << "destructing ..." << Endl; } int getInt () { return *xptr; } Private: int *xptr;}; The return value is Intnum class object Intnum Getnum () { intnum A; return A;} int main () { cout<<getnum (). GetInt () <<endl; return 0;} Running result: calling constructor ... Calling copy constructor ... Destructing ... 0Destructing ...
Example: A function returns an object containing a pointer member (version 2)
l Use the move constructor
The local object to be returned is transferred to the keynote function, eliminating the process of constructing and deleting the temporary object.
#include <iostream>using namespace Std;class intnum {public: intnum (int x = 0): xptr (new int (x)) {//constructor Co UT << "Calling constructor ..." << Endl; } Intnum (const Intnum & N): xptr (new int (*n.xptr)) {//copy constructor line Callout 3: Note:?&& is an rvalue reference? The temporary variable returned by the function is the right value cout << "Calling copy constructor ..." << Endl; } Intnum (Intnum && N): Xptr (n.xptr) { //move constructor n.xptr = nullptr; cout << "Calling move constructor ..." << Endl; } ~intnum () {//destructor delete xptr; cout << "destructing ..." << Endl; } Private: int *xptr;}; The return value is Intnum class object Intnum Getnum () {Intnum a;return A;} int main () {cout << getnum (). GetInt () << Endl; return 0;} Running result: calling constructor ... Calling move constructor ... Destructing ... 0Destructing ...
Move constructor in c++11