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<classT>classauto_ptr{private:    T*ap;public:    //constructor & destructor-----------------------------------(1)    explicitauto_ptr(T*ptr=0)throw():ap(ptr)    {    }    ~auto_ptr()throw()    {        deleteap;    }    //Copy & assignment--------------------------------------------(2)    auto_ptr(auto_ptr& rhs)throw():ap(rhs.release())    {    }    template<classY>    auto_ptr(auto_ptr<Y>&rhs)throw():ap(rhs.release())    {    }    auto_ptr& operator=(auto_ptr&rhs)throw()    {        reset(rhs.release());        return*this;    }    template<classY>    auto_ptr& operator=(auto_ptr<Y>&rhs)throw()    {        reset(rhs.release());        return*this;    }    //Dereference----------------------------------------------------(3)    T& operator*()constthrow()    {        return*ap;    }    T* operator->()constthrow()    {        returnap;    }    //Helper functions------------------------------------------------(4)    //value access    T* get()constthrow()    {        returnap;    }    //release owner ship    T* release()throw()    {        T*tmp(ap);        ap=0;        returntmp;    }    //reset value    voidreset(T*ptr=0)throw()    {        if(ap!=ptr)        {            deleteap;            ap=ptr;        }    }    //Special conversions-----------------------------------------------(5)    template<classY>    structauto_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<classY>    operator auto_ptr_ref<Y>()throw()    {        returnauto_ptr_ref<Y>(release());    }        template<classY>    operator auto_ptr<Y>()throw()    {        returnauto_ptr<Y>(release());    }}; | 
| 1 2 | int*p=newint(0);auto_ptr<int> ap(p); | 
| 1 2 3 | int*p=newint(0);auto_ptr<int>ap1(p);auto_ptr<int>ap2(p); | 
| 1 2 | int*pa=newint[10];auto_ptr<int>ap(pa); | 
| 1 2 3 4 | ~auto_ptr()throw(){    if(ap)deleteap;} | 
| 1 2 3 4 | int*p=newint(0);auto_ptr<int>ap1(p);auto_ptr<int>ap2=ap1;cout<<*ap1;//错误,此时ap1只剩一个null指针在手了 | 
| 1 2 3 4 5 6 7 8 | voidf(auto_ptr<int>ap){    cout<<*ap;}auto_ptr<int>ap1(newint(0));f(ap1);cout<<*ap1;//错误,经过f(ap1)函数调用,ap1已经不再拥有任何对象了。 | 
| 1 2 | classbase{};classderived:publicbase{}; | 
| 1 | auto_ptr<base>apbase=auto_ptr<derived>(newderived); | 
| 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 | structA{    voidf();}auto_ptr<A>apa(newA);(*apa).f();apa->f(); | 
| 1 | auto_ptr<int>ap1=auto_ptr<int>(newint(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之后来发 ... 
随机推荐
- Anniversary Party
			Time limit: 0.5 second Memory limit: 8 MB Background The president of the Ural State University is g ... 
- SAP ABAP 程序调用FORM
			*&---------------------------------------------------------------------* *& Report ZHAIM_FOR ... 
- [C#] 常用工具类——系统日志类
			using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespa ... 
- Chrome/Chromium HTML5 video 视频播放硬件加速
			Chromium站点上有个大致的框图.描写叙述了Chromium的video在各个平台 - 包含Android - 上是怎样使用硬件资源来做视频编解码加速的: 而依据Android Kitkat上的C ... 
- ViewPager的用法
			ViewPager 1.布局文件中的配置 ViewPager的路径 方法:在源码文件中输入ViewPager,按下alt+/,上面就会出来viewPager的包路径 viewPager的配置很简单,前 ... 
- STDMETHOD_,STDMETHOD,__declspec(novtable)和__declspec(selectany)
			1.STDMETHOD_(ULONG, AddRef)() PURE; STDMETHOD_:定义一个返回指定类型的虚方法, STDMETHOD:定义一个返回HRESULT类型的虚方法, PURE : ... 
- Android 5.0 新特性
			Material Design Material Design简介 Material Design是谷歌新的设计语言,谷歌希望寄由此来统一各种平台上的用户体验,Material Design的特点是干 ... 
- Maven浅析-2 什么是Maven
			1.简单点讲:Maven就是一个项目构建工具.它可以生成一个artifact(component),还可以帮我们管理项目依赖(如附加的组件Filters等). 2.从整体讲:Maven也可以看作一个项 ... 
- ( 转 )Github配置
			以下转自 http://liuzhijun.iteye.com/blog/1457207 有问题请联系我删除. -----———————————————————————— 如果你的代码不知道放哪里好, ... 
- 浅谈Mamcached集成web项目
			1.资源文件配置 config.properties 添加 #memcached服务器地址 memchchedIP=192.168.1.8 2.编写工具类 MemUtils package cn.co ... 
