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 ...
随机推荐
- sqoop的merge和eval 工具
1.sqoop的merge的工具 sqoop merge 可以将hdfs上的两个文件进行合并,在increment import的过程中经常会用到,如incremenet import将数据导入到hd ...
- 《Java中的抽象类及抽象类的作用》
//抽象类示例: abstract class Shape1 { { System.out.println("执行Shape的初始化块..."); } private String ...
- struts2 log4j:WARN Please initialize the log4j system properly. 解决方法
在tomcat启动的时候,出现这个警告: log4j:WARN No appenders could be found for logger (org.apache.commons.digester. ...
- 浅谈对ECharts的使用
上个月的项目,其中有一个模块用的是ECharts来实现的,分别用了折线图,环形图,还有漏斗图,这几个都算比较常见的了,尤其是折线图,环形图,用的最多的就是它们了.之前也没怎么接触过ECharts,实际 ...
- nl2br() 函数
nl2br() 函数在字符串中的每个新行 (\n) 之前插入 HTML 换行符 (<br />).经常用于在线编辑的内容,以便显示. 用法:nl2br(string) 参数 描述 stri ...
- [GodLove]Wine93 Tarining Round #1
比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44664#overview 题目来源: 2011 Asia Regional ...
- day5--<装饰器、模块、字符串格式化、生成器、迭代器>logging模块
本节大纲: 一:双层装饰器:一个函数可以被多层装饰器进行装饰,函数渲染(编译)从下到上,函数执行从上到下.如下程序: #!/usr/bin/env python #-*-coding:utf-8-*- ...
- Java 设计一个贷款计算器 简易
import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.border.*; publi ...
- unity之mipmap
Mipmap技术有点类似于LOD技术,但是不同的是,LOD针对的是模型资源,而Mipmap针对的纹理贴图资源 使用Mipmap后,贴图会根据摄像机距离的远近,选择使用不同精度的贴图. 缺点:会占用内存 ...
- pods的安装和使用
//// pods的安装.h// IOS笔记 /*Cocoapods安装步骤 1.升级Ruby环境 终端输入:$gem update --system 此时会出现 ERROR: While ex ...