C++0x尝鲜:移动语义(Move Semantics)

MSDN例子
// 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.
};
代码说明
  1. MemoryBlock是一个自动管理整形动态数组的类。
  2. MemoryBlock类只有两个数据成员:整形数组的长度_length以及首地址指针_data。
  3. MemoryBlock类共有六个特殊成员函数,
    其中移动构造器以及移动赋值运算符为C++0x标准所新增的特殊成员函数。
    详细说明可见下表。
成员函数名位置参数功能
初始化构造器第11~17行整形长度length设置数组长度并分配内存
析构器第20~33行检查数组是否为空,如不为空,则释放内存
拷贝构造器第36~44行相同类型的const左值引用other拷贝other的内容以完成构造
1.将数组长度设置为other的长度
2.分配与other相同大小的内存
3.拷贝other的数组元素
拷贝赋值运算符第47~62行相同类型的const左值引用other拷贝other的内容以完成赋值
功能上大致相当于析构器+拷贝构造器
移动构造器第65~84行相同类型的右值引用other移动(窃取)other的内容以完成构造
1.将数组长度设置为other的数组长度
2.将数组首地址指针设置为other的数组首地址指针
3.将other的数组长度清0
4.将other的数组首地址指针设置为空指针
移动赋值运算符第87~108行相同类型的右值引用other移动(窃取)other的内容以完成赋值
功能上大致相当于析构器+移动构造器

测试代码:
#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));
}
Windows平台下使用gcc4.6.1编译运行结果如下:
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.
如果从MemoryBlock类中删除移动构造器以及移动赋值运算符,重新编译运行结果如下:
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.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值