c++11右值引用与性能优化

c++11中增加了右值引用,

相关文章自行百度,csdn里很多,其中,std::unique_ptr 的实现就是使用了std::move

 

#include <iostream>
#include <vector>

#pragma warning(disable : 4996)

using namespace std;
#define PRINT(a) cout << "行" << __LINE__ << "调用:"; a

class Base
{
public :
	int a;
	char * buf = NULL;

	void SetStr(const char * buffer)
	{
		if (!buffer)
			return;

		if (buf)
			delete[] buf;

		buf = new char[strlen(buffer) + 1];
		strcpy(buf, buffer);

	}
public:
	Base() : a(1)
	{
		cout << "构造0-行" << __LINE__  << "\n";
	}
	Base(int i) : a(i)
	{
		cout << "构造1-行" << __LINE__ << "\n";
	}
	virtual ~Base()
	{
		cout << "析构\n";
		if (buf)
			delete[] buf;
	}

public :
	Base(const Base& other) : a(other.a)
	{
		SetStr(other.buf);
		cout << "拷贝构造-行" << __LINE__ << "\n";
	}

	Base & operator=(const Base& other){
		this->a = other.a;
		SetStr(other.buf);
		cout << "拷贝赋值-行" << __LINE__ << "\n";
		return *this;
	}

	Base(Base&& other)  //  这里不能是const类型,因为可能要更改other,
	{
		this->a = other.a;
		this->buf = other.buf;
		other.buf = NULL;         // 交接堆数据空间
		cout << "转移构造-行" << __LINE__ << "\n";
	}

	Base & operator=(Base&& other) {
		this->a = other.a;
		this->buf = other.buf;
		other.buf = NULL;         // 交接堆数据空间
		cout << "转移赋值-行" << __LINE__ << "\n";
		return *this;
	}

public:

	int getInt()
	{
		return a;
	}

	int && getRvalueInt()
	{
		// notice that it's fine to move a primitive type--remember, std::move is just a cast  
		return std::move(a);
	}

};

void test1()
{
	Base b1;     // 构造0

//Base b2 = Base(1);  // 构造1
	Base b2(1);   // 构造1

	Base b3 = b1;  // 拷贝构造

	Base b4(b1); // 拷贝构造

	b4 = b3;     // 赋值
}

void test0()
{
	Base b3 = Base(1);  // 构造

	Base b4 = b3;              // 拷贝构造
	Base b5 = std::move(b3);   // 转移构造

	b4 = std::move(b5);        // 转移赋值
}

void test2()
{
	std::vector<Base> list;

	PRINT(Base b;)             

	//PRINT(list.push_back(b);)        // 执行拷贝构造
	//PRINT(list.push_back( move(b) );)  // 转移构造

	PRINT(list.push_back(Base(5));)  // 构造临时对象,转移构造添加,临时对象析构
}



int main()
{
	test2();

	char m;
	std::cin >> m;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++性能优化 指南(强列推荐) chm版 Part I: Everything But the Code Chapter 1. Optimizing: What Is It All About? Performance Footprint Summary Chapter 2. Creating a New System System Requirements System Design Issues The Development Process Data Processing Methods Summary Chapter 3. Modifying an Existing System Identifying What to Modify Beginning Your Optimization Analyzing Target Areas Performing the Optimizations Summary Part II: Getting Our Hands Dirty Chapter 4. Tools and Languages Tools You Cannot Do Without Optimizing with Help from the Compiler The Language for the Job Summary Chapter 5. Measuring Time and Complexity The Marriage of Theory and Practice System Influences Summary Chapter 6. The Standard C/C++ Variables Variable Base Types Grouping Base Types Summary Chapter 7. Basic Programming Statements Selectors Loops Summary Chapter 8. Functions Invoking Functions Passing Data to Functions Early Returns Functions as Class Methods Summary Chapter 9. Efficient Memory Management Memory Fragmentation Memory Management Resizable Data Structures Summary Chapter 10. Blocks of Data Comparing Blocks of Data The Theory of Sorting Data Sorting Techniques Summary Chapter 11. Storage Structures Arrays Linked Lists Hash Tables Binary Trees Red/Black Trees Summary Chapter 12. Optimizing IO Efficient Screen Output Efficient Binary File IO Efficient Text File IO Summary Chapter 13. Optimizing Your Code Further Arithmetic Operations Operating System–Based Optimizations Summary Part III: Tips and Pitfalls Chapter 14. Tips Tricks Preparing for the Future Chapter 15. Pitfalls Algorithmic Pitfalls Typos that Compile Other Pitfalls
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值