设为首页 加入收藏

TOP

c++ 11学习笔记--右值引用和移动构造语义(一)
2015-07-20 18:04:58 来源: 作者: 【 】 浏览:3
Tags:学习 笔记 引用 移动 构造 语义
今天我决定尝试用另外一种方式来表达,任何新语法的的产生都是为了解决某个问题,所以今天先看问题。
?
复制代码
class myStr
{
protected:
? ? char* str_;
? ??
public:
? ? myStr(void) ? ? ? ? ? ? ? ? ? ? ? // 默认的构造函数,什么也不做
? ? : str_(nullptr)
? ? {}
? ??
? ? myStr(const char* rhs) ? ? ? ? ? ?// 普通赋值构造函数
? ? : str_(nullptr)
? ? {
? ? ? ? if (!rhs) return;
? ? ? ? str_ = new char[1024];
? ? ? ? strcpy(str_, rhs);
? ? ? // ?cout << "Str constructor " << str_ << std::endl;
? ? }
? ??
? ? myStr(const myStr& rhs) ? ? ? ? ? ? // 拷贝构造函数
? ? : str_(nullptr)
? ? {
? ? ? ? if (!rhs) return;
? ? ? ? str_ = new char[1024];
? ? ? ? strcpy(str_, rhs.str_);
? ? ? // ?cout << "Str copy constructor " << str_ << std::endl;
? ? }
? ??
? ? myStr(myStr&& rhs)
? ? : str_(nullptr)
? ? {
? ? ? ? swap(rhs);
? ? ? // ?std::cout << "Str move constructor " << str_ << std::endl;
? ? }
? ??
? ? ~myStr() ? ? ? ? ? ? ? ? ? ? ? ? ?// 析构函数
? ? {
? ? ? ? if (!str_) return;
? ? ? // ?std::cout << "Str destructor " << str_ << std::endl;
? ? ? ? delete [] str_;
? ? }
? ??
? ? const myStr& operator=(myStr rhs) ? // 赋值操作符重载
? ? {
? ? ? ? rhs.swap(*this); ? ? ? ? ? ?// 使用copy-and-swap惯用法获得数据
? ? ? ? return (*this); ? ? ? ? ? ? // 避免重复撰写operator=
? ? }
? ??
? ? void swap(myStr& rhs) ? ? ? ? ? ? // 交换算法
? ? {
? ? ? ? std::swap(str_, rhs.str_);
? ? }
? ??
? ? operator char*(void) const
? ? {
? ? ? ? return str_;
? ? }
? ??
? ? myStr& operator+=(const char* rhs)
? ? {
? ? ? ? if (rhs) strcat(str_, rhs);
? ? ? ? return (*this);
? ? }
? ??
// ? ?friend myStr operator+(const myStr& x, const myStr& y)
// ? ?{
// ? ? ? ?return myStr(x) += y;
// ? ?}
? ? friend myStr operator+(const myStr& x, const myStr& y)
? ? {
? ? ? ? return std::move(myStr(x) += y);
}
复制代码
执行下面的代码
?
复制代码
myStr ss("000");
? ? myStr s1("11111"), s2("22222"), s3("3333333"), s4("4444444");
? ? cout << std::endl;
? ? time_t timestamp1;
? ? time_t timestamp2;
? ? time_t timestamp3;
? ??
? ? const long long max = 30000000;
? ? time(×tamp1);
?
? ? for (long long i = 0; i
? ? ? ? ss = s1 + s2 + s3 + s4;
? ? }
? ? ?time(×tamp2);
? ??
?timestamp3 = timestamp2 - timestamp1;
复制代码
?下面的代码是唯一不同的实现,但是却带来30-40%的性能差距。
?
复制代码
/ ? ?friend myStr operator+(const myStr& x, const myStr& y)
// ? ?{
// ? ? ? ?return myStr(x) += y;
// ? ?}
? ? friend myStr operator+(const myStr& x, const myStr& y)
? ? {
? ? ? ? return std::move(myStr(x) += y);
}
复制代码
再找一个例子
?
复制代码
class MemoryBlock
{
public:
? ??
? ? // 构造器(初始化资源)
? ? explicit MemoryBlock(size_t length)
? ? : _length(length)
? ? , _data(new int[length])
? ? {
? ? ? ? std::cout << "MemoryBlock constructor " ?<< std::endl;
? ? }
? ??
? ? // 析构器(释放资源)
? ? ~MemoryBlock()
? ? {
? ? ? ? if (_data != nullptr)
? ? ? ? {
? ? ? ? ? ? delete[] _data;
? ? ? ? }
? ? ? ? std::cout << "MemoryBlock destructor " ?<< std::endl;
? ? }
? ??
? ? // 拷贝构造器(实现拷贝语义:拷贝that)
? ? MemoryBlock(const MemoryBlock& that)
? ? // 拷贝that对象所拥有的资源
? ? : _length(that._length)
? ? , _data(new int[that._length])
? ? {
? ? ? ? std::copy(that._data, that._data + _length, _data);
? ? ? ? std::cout << "copy constructor " ?<< std::endl;
? ? }
? ??
? ? // 拷贝赋值运算符(实现拷贝语义:释放this + 拷贝that)
? ? MemoryBlock& operator=(const MemoryBlock& that)
? ? {
? ? ? ? if (this != &that)
? ? ? ? {
? ? ? ? ? ? // 释放自身的资源
? ? ? ? ? ? delete[] _data;
? ? ? ? ? ??
? ? ? ? ? ? // 拷贝that对象所拥有的资源
? ? ? ? ? ? _length = that._length;
? ? ? ? ? ? _data = new int[_length];
? ? ? ? ? ? std::copy(that._data, that._data + _length, _data);
? ? ? ? }
? ? ? ? return *this;
? ? }
? ??
? ? // 移动构造器(实现移动语义:移动that)
? ? MemoryBlock(MemoryBlock&& that)
? ? // 将自身的资源指针指向that对象所拥有的资源
? ? : _length(that._length)
? ? , _data(that
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇用block实现两个页面间的传值 下一篇代理模式 + Effective C++ 第一章..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: