The move constructor in C++11

Source: Internet
Author: User

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

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.