C ++ 0x: Move Semantics)

Source: Internet
Author: User
Msdn example
// MemoryBlock.h#pragma once#include <iostream>#include <algorithm>class MemoryBlock{public:// Simple constructor that initializes the resource.explicit MemoryBlock(size_t length): _length(length), _data(new int[length]){std::cout << "In MemoryBlock(size_t). length = "<< _length << "." << std::endl;}// Destructor.~MemoryBlock(){std::cout << "In ~MemoryBlock(). length = "<< _length << ".";if (_data != nullptr){std::cout << " Deleting resource.";// Delete the resource.delete[] _data;}std::cout << std::endl;}// Copy constructor.MemoryBlock(const MemoryBlock& other): _length(other._length), _data(new int[other._length]){std::cout << "In MemoryBlock(const MemoryBlock&). length = " << other._length << ". Copying resource." << std::endl;std::copy(other._data, other._data + _length, _data);}// Copy assignment operator.MemoryBlock& operator=(const MemoryBlock& other){std::cout << "In operator=(const MemoryBlock&). length = " << other._length << ". Copying resource." << std::endl;if (this != &other){// Free the existing resource.delete[] _data;_length = other._length;_data = new int[_length];std::copy(other._data, other._data + _length, _data);}return *this;}// Move constructor.MemoryBlock(MemoryBlock&& other): _data(nullptr), _length(0){std::cout << "In MemoryBlock(MemoryBlock&&). length = " << other._length << ". Moving resource." << std::endl;// Copy the data pointer and its length from the // source object._data = other._data;_length = other._length;// Release the data pointer from the source object so that// the destructor does not free the memory multiple times.other._data = nullptr;other._length = 0;// revised version// *this = std::move(other);}// Move assignment operator.MemoryBlock& operator=(MemoryBlock&& other){std::cout << "In operator=(MemoryBlock&&). length = " << other._length << "." << std::endl;if (this != &other){// Free the existing resource.delete[] _data;// Copy the data pointer and its length from the // source object._data = other._data;_length = other._length;// Release the data pointer from the source object so that// the destructor does not free the memory multiple times.other._data = nullptr;other._length = 0;}return *this;}// Retrieves the length of the data resource.size_t Length() const{return _length;}private:size_t _length; // The length of the resource.int* _data; // The resource.};

Code Description

  1. Memoryblock is a class that automatically manages integer dynamic arrays.
  2. The memoryblock class has only two data members: the length of the integer array _ length and the first address pointer _ data.
  3. The memoryblock class has six special member functions,
    The moving constructor and the moving value assignment operator are special member functions added to the C ++ 0x standard.
    The following table lists detailed descriptions.
Member function name Location Parameters Function
Initialize the constructor 11th ~ 17 rows Integer Length Set array length and allocate memory
Destructor 20th ~ 33 rows None Check whether the array is empty. If it is not empty, the memory is released.
Copy constructor 36th ~ 44 rows Reference other for the left value of const of the same type Copy other content to complete construction
1. Set the array length to the other length.
2. allocate memory of the same size as other
3. Copy the array elements of other
Copy assignment operator 47th ~ 62 rows Reference other for the left value of const of the same type Copy other content to Complete Assignment
The function is roughly equivalent to the Destructor + copy constructor.
Mobile Constructor 65th ~ 84 rows The right value of the same type references Other. Mobile (stealing) Other content to complete construction
1. Set the array length to the other array length.
2. Set the first address pointer of the array to the first address pointer of the other array.
3. Clear the other array length by 0
4. Set the first address pointer of the other array to a null pointer.
MobileAssignmentOperator 87th ~ 108 rows The right value of the same type references Other. Move (steal) Other content to Complete Assignment
The function is roughly equivalent to the Destructor + mobile constructor.

Test code:

#include "MemoryBlock.h"#include <vector>using namespace std;int main(){   // Create a vector object and add a few elements to it.   vector<MemoryBlock> v;   v.push_back(MemoryBlock(25));   v.push_back(MemoryBlock(75));   // Insert a new element into the second position of the vector.   v.insert(v.begin() + 1, MemoryBlock(50));}

On Windows, gcc4.6.1 is compiled and run as follows:

In MemoryBlock(size_t). length = 25.In MemoryBlock(MemoryBlock&&). length = 25. Moving resource.In ~MemoryBlock(). length = 0.In MemoryBlock(size_t). length = 75.In MemoryBlock(MemoryBlock&&). length = 75. Moving resource.In MemoryBlock(MemoryBlock&&). length = 25. Moving resource.In ~MemoryBlock(). length = 0.In ~MemoryBlock(). length = 0.In MemoryBlock(size_t). length = 50.In MemoryBlock(MemoryBlock&&). length = 50. Moving resource.In MemoryBlock(MemoryBlock&&). length = 25. Moving resource.In MemoryBlock(MemoryBlock&&). length = 75. Moving resource.In ~MemoryBlock(). length = 0.In ~MemoryBlock(). length = 0.In ~MemoryBlock(). length = 0.In ~MemoryBlock(). length = 25. Deleting resource.In ~MemoryBlock(). length = 50. Deleting resource.In ~MemoryBlock(). length = 75. Deleting resource.

If you delete the mobile constructor and the mobile assignment operator from the memoryblock class, recompile the execution result as follows:

In MemoryBlock(size_t). length = 25.In MemoryBlock(const MemoryBlock&). length = 25. Copying resource.In ~MemoryBlock(). length = 25. Deleting resource.In MemoryBlock(size_t). length = 75.In MemoryBlock(const MemoryBlock&). length = 75. Copying resource.In MemoryBlock(const MemoryBlock&). length = 25. Copying resource.In ~MemoryBlock(). length = 25. Deleting resource.In ~MemoryBlock(). length = 75. Deleting resource.In MemoryBlock(size_t). length = 50.In MemoryBlock(const MemoryBlock&). length = 50. Copying resource.In MemoryBlock(const MemoryBlock&). length = 25. Copying resource.In MemoryBlock(const MemoryBlock&). length = 75. Copying resource.In ~MemoryBlock(). length = 25. Deleting resource.In ~MemoryBlock(). length = 75. Deleting resource.In ~MemoryBlock(). length = 50. Deleting resource.In ~MemoryBlock(). length = 25. Deleting resource.In ~MemoryBlock(). length = 50. Deleting resource.In ~MemoryBlock(). length = 75. Deleting resource.

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.