auto_ptr, which can release the space automatically
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
template<class T>class auto_ptr{private: T*ap;public: //constructor & destructor-----------------------------------(1) explicit auto_ptr(T*ptr=0)throw():ap(ptr) { } ~auto_ptr()throw() { delete ap; } //Copy & assignment--------------------------------------------(2) auto_ptr(auto_ptr& rhs)throw():ap(rhs.release()) { } template<class Y> auto_ptr(auto_ptr<Y>&rhs)throw():ap(rhs.release()) { } auto_ptr& operator=(auto_ptr&rhs)throw() { reset(rhs.release()); return*this; } template<class Y> auto_ptr& operator=(auto_ptr<Y>&rhs)throw() { reset(rhs.release()); return*this; } //Dereference----------------------------------------------------(3) T& operator*()const throw() { return*ap; } T* operator->()const throw() { returnap; } //Helper functions------------------------------------------------(4) //value access T* get()const throw() { returnap; } //release owner ship T* release()throw() { T*tmp(ap); ap=0; return tmp; } //reset value void reset(T*ptr=0)throw() { if(ap!=ptr) { deleteap; ap=ptr; } } //Special conversions-----------------------------------------------(5) template<class Y> struct auto_ptr_ref { Y*yp; auto_ptr_ref(Y*rhs):yp(rhs){} }; auto_ptr(auto_ptr_ref<T>rhs)throw():ap(rhs.yp) { } auto_ptr& operator=(auto_ptr_ref<T>rhs)throw() { reset(rhs.yp); return*this; } template<class Y> operator auto_ptr_ref<Y>()throw() { returnauto_ptr_ref<Y>(release()); } template<class Y> operator auto_ptr<Y>()throw() { returnauto_ptr<Y>(release()); }}; |
|
1
2
|
int*p=new int(0);auto_ptr<int> ap(p); |
|
1
2
3
|
int*p=new int(0);auto_ptr<int>ap1(p);auto_ptr<int>ap2(p); |
|
1
2
|
int*pa=new int[10];auto_ptr<int>ap(pa); |
|
1
2
3
4
|
~auto_ptr()throw(){ if(ap)delete ap;} |
|
1
2
3
4
|
int*p=new int(0);auto_ptr<int>ap1(p);auto_ptr<int>ap2=ap1;cout<<*ap1;//错误,此时ap1只剩一个null指针在手了 |
|
1
2
3
4
5
6
7
8
|
void f(auto_ptr<int>ap){ cout<<*ap;}auto_ptr<int>ap1(new int(0));f(ap1);cout<<*ap1;//错误,经过f(ap1)函数调用,ap1已经不再拥有任何对象了。 |
|
1
2
|
class base{};class derived:public base{}; |
|
1
|
auto_ptr<base>apbase=auto_ptr<derived>(new derived); |
|
1
2
3
4
5
6
|
A a1;A a2(a1);A a3;a3=a1;//那么a2==a1,a3==a1 |
|
1
2
3
4
5
6
7
8
|
struct A{ void f();}auto_ptr<A>apa(new A);(*apa).f();apa->f(); |
|
1
|
auto_ptr<int>ap1=auto_ptr<int>(new int(0)); |
auto_ptr, which can release the space automatically的更多相关文章
- iPhone:4.7 5.5 4 3.5 对应的各个设备屏幕尺寸对应的像素及App上线信息
Shared App Information You can access these properties from the App Details page in the App Informat ...
- ORA-15028: ASM file '..' not dropped; currently being accessed --转载
Couple of weeks ago we had a problem with one of our busiest databases. The FRA was filling quite ra ...
- Why do we need smart pointer and how to implement it.
Here are two simple questions. Problem A #include <string> include <iostream> using name ...
- EBS Archiving and Purging: You Know you need to
A number of trends in the IT industry have contributed to the increasing size of ERP application dat ...
- page size
https://dev.mysql.com/doc/refman/5.7/en/glossary.html#glos_page_size https://dev.mysql.com/doc/refma ...
- 1Z0-053 争议题目解析704
1Z0-053 争议题目解析704 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 704.View the Exhibit and examine the data manipul ...
- 【ubuntu】install openbox+tint2+bmenu on ubuntu12.04.4
原文地址: http://ndever.net/articles/linux/install-openbox-ubuntu-1304-1310 openbox是我用过的轻量窗口中最好用的了. Step ...
- C++11智能指针
今晚跟同学谈了一下智能指针,突然想要看一下C++11的智能指针的实现,因此下了这篇博文. 以下代码出自于VS2012 <memory> template<class _Ty> ...
- Android & Eclipse FAQ
一.eclipse中格式化代码快捷键Ctrl+Shift+F失效的解决办法 当我要格式化代码的时候,右键-source-format能够起效,但ctrl+shift+f不好使了. google之后来发 ...
随机推荐
- <%%>与<%=%>区别
1.<%= %>只能得到当前面定义的值.即你在这个页里有<% int a=100%>--你在下面才可以使用<%=a%>2.${ }这个是el表达式.可以从上下文中得 ...
- SQL基础之 时间戳
本文转载:http://www.cnblogs.com/liuhh/archive/2011/05/14/2046544.html 一直对时间戳这个概念比较模糊,相信有很多朋友也都会误认为:时间戳是一 ...
- 80端口被system占用的问题
80端口被system占用的问题今天启动Apache的时候发现无法启动-80端口被占用. 检查进程发现是system进程id=4给占用了.这个是系统进程 检查服务,看看IIS.没发现问题. 检查了半天 ...
- 购买SSD固态硬盘须当心,你知道什么是SLC、 MLC、TLC闪存芯片颗粒吗?
固态硬盘凭借其存取速率超快等自身优势,被越来越多的电脑爱好者所青睐,并迅速普及到了广大用户的电脑中,因为固态硬盘与传统机械硬盘相比,确实在运行效率等方面有了质的提升,但是亦是美网络小编要提醒大家的是, ...
- linux下查找文件
1,find 经常在linux下工作,总要查找一些文件,于是就搜索的学习了一下 find 指定目录 指定条件 指定动作 举例:find . -name "my*" 查找 当前目录下 ...
- XML的基本操作
所有 XML 文档中的文本均会被解析器解析.只有 CDATA 区段(CDATA section)中的文本会被解析器忽略.CDATA 部分中的所有内容都会被解析器忽略.CDATA 部分由 "& ...
- 模版引擎(NVelocity)开发
在net中用模版开发,在handler中用到了大量的html代码.为解决这个问题,我可以采用模版引擎(NVelocity)进行开发.1.首先需要将NVelocity.dll文件放入项目,其次引用.2. ...
- C# 文件粉碎
>文件粉碎的流程 填充文件 更改最后使用/创建时间 更改名称 将大小改为 0 kb 删除 using System; using System.Collections.Generic; usin ...
- 使用Druid作为数据源
Druid号称是Java语言中最好的数据库连接池. 1) 可以监控数据库访问性能,Druid内置提供了一个功能强大的StatFilter插件,能够详细统计SQL的执行性能,这对于线上分析数据库访问性能 ...
- Js 命名空间注册方法
MyApp = { namespace: function () { var a = arguments, o = null, i, j, d, rt; for (i = 0; i < a.le ...