2016-08-16: copy-and-swap
#include <algorithm> // std::copy
#include <cstddef> // std::size_t
#include <stdio.h> class dumb_array
{
public:
// (default) constructor
dumb_array(std::size_t size = 0)
: mSize(size),
mArray(mSize ? new int[mSize]() : 0)
{
printf("construct %p\n", this);
} // copy-constructor
dumb_array(const dumb_array& other)
: mSize(other.mSize),
mArray(mSize ? new int[mSize] : 0)
{
// note that this is non-throwing, because of the data
// types being used; more attention to detail with regards
// to exceptions must be given in a more general case, however
printf("copy-constructor %p\n", this);
std::copy(other.mArray, other.mArray + mSize, mArray);
} friend void swap(dumb_array& first, dumb_array& second) // nothrow
{
// enable ADL (not necessary in our case, but good practice)
using std::swap;
// by swapping the members of two classes,
// the two classes are effectively swapped
swap(first.mSize, second.mSize);
swap(first.mArray, second.mArray);
} dumb_array& operator=(dumb_array other) // (1)
{
printf("other %p, operator= %p\n", &other, this);
swap(*this, other); // (2)
return *this;
} // destructor
~dumb_array()
{
printf("destruct %p\n", this);
delete [] mArray;
} private:
std::size_t mSize;
int* mArray;
}; int main()
{
dumb_array obj(100);
printf("test copy-constructor\n");
dumb_array copy_obj(obj); printf("test operator=\n");
dumb_array operator_obj;
operator_obj = obj; return 0;
} /*
construct 0x7fffdd3b4730
test copy-constructor
copy-constructor 0x7fffdd3b4720
test operator=
construct 0x7fffdd3b4710
copy-constructor 0x7fffdd3b4740
other 0x7fffdd3b4740, operator= 0x7fffdd3b4710
destruct 0x7fffdd3b4740
destruct 0x7fffdd3b4710
destruct 0x7fffdd3b4720
destruct 0x7fffdd3b4730
*/
参考资料
What is the copy-and-swap idiom?
2016-08-16: copy-and-swap的更多相关文章
- Murano Weekly Meeting 2016.08.16
Meeting time: 2016.August.16 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: ...
- mysql查询练习题-2016.12.16
>>>>>>>>>> 练习时间:2016.12.16 编辑时间:2016-12-20-->22:12:08 题: 涉及:多表查询.ex ...
- 2016.8.16上午纪中初中部NOIP普及组比赛
2016.8.16上午纪中初中部NOIP普及组比赛 链接:https://jzoj.net/junior/#contest/home/1334 这次也翻车了,感觉比之前难多了. 辛辛苦苦改完了,太难改 ...
- http://tedhacker.top/2016/08/05/Spring%E7%BA%BF%E7%A8%8B%E6%B1%A0%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95/
http://tedhacker.top/2016/08/05/Spring%E7%BA%BF%E7%A8%8B%E6%B1%A0%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%9 ...
- c++异常安全和copy and swap策略
异常安全有两个目标: 不泄露任何资源.这个通过RAII可以做到. 不破坏数据结构.这是下文要讨论的事情 异常安全有三个级别: 基本安全:异常发生后对象和数据结构还有合法状态.实现简单,应该作为最低要求 ...
- 【转载】webstorm11(注册,激活,破解,码,一起支持正版,最新可用)(2016.11.16更新)
很多人都发现 http://idea.lanyus.com/ 不能激活了 很多帖子说的 http://15.idea.lanyus.com/ 之类都用不了了 最近封的厉害仅作测试 选择 License ...
- http://www.cnblogs.com/alipayhutu/archive/2012/08/16/2643098.html
http://www.cnblogs.com/alipayhutu/archive/2012/08/16/2643098.html
- C++异常安全、copy and swap
异常安全的代码是指,满足两个条件 1异常中立性 : 是指当你的代码(包括你调用的代码)引发异常时,这个异常 能保持原样传递到外层调用代码.(异常中立,就是指任何底层的异常都会抛出到上层,也就相当于是异 ...
- copy and swap技巧与移动赋值操作符
最近在实现一个Delegate类的时候碰到了一个问题,就是copy and swap技巧和移动赋值操作符有冲突. 比如有以下一个类: class Fun { public: Fun(const Fun ...
- 最新版Theos.2016.08的安装方法
http://bbs.pediy.com/showthread.php?t=212425 标题: [翻译]手把手安装最新版Theos.2016.08作者: roysue时间: 2016-08-26,1 ...
随机推荐
- Python开发入门与实战11-单元测试
11. 单元测试 本章节我们来讲讲django工程中如何实现单元测试,单元测试如何编写以及在可持续项目中单元测试的重要性. 下面是单元测试的定义: 单元测试是开发者编写的一小段代码,用于检验被测代码的 ...
- 探索javascript----我对渐变轮播图的理解
对于一个没有编程基础的人来说,我时常希望能有人告诉我,当我们看到一个效果的时候,该怎样有条理地分析出它的行为,而我自己有必要加强这方面的 自省,对于一个轮播图我是这样看的,自动播放必然带有一个定时器, ...
- Ubuntu中查看32还是64
安装ubuntu在pc上,不推荐在32位pc安装64位操作系统,64位pc安装32位操作系统 方法/步骤 1 按ctrl+shift+t 快捷键,打开终端,输入sudo uname --m ,按下 ...
- Python Scopes and Namespaces
Before introducing classes, I first have to tell you something about Python's scope rules. Class def ...
- 北京网赛I题 hiho1391 (树状数组、区间覆盖最大值问题)
题目链接:http://hihocoder.com/problemset/problem/1391 题意:A国和B国向对方分别投射N枚和M枚导弹(发射时间,飞行时间,伤害值),同时两国各自都有防御系统 ...
- Unity Sprite切割导出
这次需要将美术提供的Sprite图集切割导出,整体思路依然和上次的Sprite转prefab一致,只是在转prefab的逻辑修改为了创建Texture的逻辑. 过程很简单,直接看最终代码结果: usi ...
- overflow 清除浮动
对overflow的理解还停留在“规定当内容溢出元素框时发生的事情”,这种简单的认识上,今天发现overflow还可以清除浮动. <style> .outside{ height:auto ...
- SQL 函数集锦
..STUFF()用另一子串替换字符串指定位置.长度的子串.STUFF (<character_expression1>, <start_ position>, <len ...
- Flume 实战(1) -- 初体验
前言: Flume-ng是数据收集/聚合/传输的组件, Flume-ng抛弃了Flume OG原本繁重的zookeeper和Master, Collector, 其整体的架构更加的简洁和明了. 其基础 ...
- 【 D3.js 入门系列 --- 4 】 如何使用scale(比例)
在上一节中使用了一个很重要的概念 — scale (这个不知道翻译成什么,暂且叫它比例).本节将重点介绍它的相关使用方法. 在介绍 scale 之前,先介绍两个经常和 scale 一起出现的函数,在上 ...