C++ 智能指针auto_ptr
template<class T>
class auto_ptr {
public:
explicit auto_ptr(T *p = ); // Item M5 有“explicitfor”// 的描述
template<class U> // 拷贝构造函数成员模板
auto_ptr(auto_ptr<U>& rhs); // (见Item M28):
// 用另一个类型兼容的
// auto_ptr对象
// 初始化一个新的auto_ptr对象 ~auto_ptr();
template<class U> // 赋值操作成员模板
auto_ptr<T>& // (见Item M28):
operator=(auto_ptr<U>& rhs); // 用另一个类型兼容的 // auto_ptr对象给它赋值
T& operator*() const; // 见Item M28
T* operator->() const; // 见Item M28
T* get() const; // 返回包容指针的
// 当前值
T* release(); // 放弃包容指针的
// 所有权,
// 并返回其当前值
void reset(T *p = ); // 删除包容指针,
// 获得指针p的所有权
private:
T *pointee; template<class U> // 让所有的auto_ptr类
friend class auto_ptr<U>; // 成为友元 };
//构造函数
template<class T>
inline auto_ptr<T>::auto_ptr(T *p)
: pointee(p)
{}
//拷贝构造函数
template<class T>
inline auto_ptr<T>::auto_ptr(auto_ptr<U>& rhs)
: pointee(rhs.release())
{} template<class T>
inline auto_ptr<T>::~auto_ptr()
{ delete pointee; } template<class T>
template<class U>
inline auto_ptr<T>& auto_ptr<T>::operator=(auto_ptr<U>& rhs)
{
if (this != &rhs) reset(rhs.release());
return *this;
} template<class T>
inline T& auto_ptr<T>::operator*() const
{ return *pointee; } template<class T>
inline T* auto_ptr<T>::operator->() const
{ return pointee; } template<class T>
inline T* auto_ptr<T>::get() const
{ return pointee; } template<class T>
inline T* auto_ptr<T>::release()
{
T *oldPointee = pointee;
pointee = ;
return oldPointee;
} template<class T>
inline void auto_ptr<T>::reset(T *p)
{
if (pointee != p) {
delete pointee;
pointee = p;
}
}
没看过stl源码剖析,感觉这个智能指针只是在自己要管理的内存资源上封装成类,这样利用对象解析时自动调用析构函数的思想,来保证delete的执行。
//摘自网上http://dbchu.com/2013/05/11/966
STL中智能指针 auto_ptr(C++98)所做的事情,就是动态分配对象以及当对象不再需要时自动执行清理。
但是auto_ptr有缺陷,在使用时要注意避免。
不要将auto_ptr对象作为STL容器的元素。C++标准明确禁止这样做,否则可能会碰到不可预见的结果。
1、auto_ptr不能共享所有权。
2、auto_ptr不能指向数组
3、auto_ptr不能作为容器的成员。
4、不能通过赋值操作来初始化auto_ptr
std::auto_ptr<int> p(new int(42)); //OK
std::auto_ptr<int> p = new int(42); //ERROR
这是因为auto_ptr 的构造函数被定义为了explicit
5、不要把auto_ptr放入容器
C++ 智能指针auto_ptr的更多相关文章
- C++智能指针(auto_ptr)详解
智能指针(auto_ptr) 这个名字听起来很酷是不是?其实auto_ptr 只是C++标准库提供的一个类模板,它与传统的new/delete控制内存相比有一定优势,但也有其局限.本文总结的8个问题足 ...
- 自己动手实现智能指针auto_ptr
面试的时候,我们经常会被问到如何自己动手实现智能指针auto_ptr.今天我就一边参考STL库中的源代码,一边将auto_ptr的实现敲一遍. auto_ptr归根到底是一个模版类,那么这个类要实现哪 ...
- 关于智能指针auto_ptr
智能指针auto_ptr和shared_ptr也是面试中经常被问到的一个 感觉看auto_ptr的源码反而更加容易理解一些,因为源码的代码量并不大,而且比较容易理解. 本篇主要介绍auto_ptr 其 ...
- C++中的智能指针(auto_ptr)
实际上auto_ptr 仅仅是C++标准库提供的一个类模板,它与传统的new/delete控制内存相比有一定优势.使用它不必每次都手动调用delete去释放内存.当然有利也有弊,也不是全然完美的. 本 ...
- 【C++】智能指针auto_ptr简单的实现
//[C++]智能指针auto_ptr简单的实现 #include <iostream> using namespace std; template <class _Ty> c ...
- 智能指针auto_ptr & shared_ptr
转载:智能指针auto_ptr 很多人听说过标准auto_ptr智能指针机制,但并不是每个人都天天使用它.这真是个遗憾,因为auto_ptr优雅地解决了C++设计和编码中常见的问题,正确地使用它可以生 ...
- C++智能指针 auto_ptr
C++智能指针 auto_ptr auto_ptr 是一个轻量级的智能指针, 定义于 memory (非memory.h)中, 命名空间为 std. auto_ptr 适合用来管理生命周期比较短或者不 ...
- C++智能指针--auto_ptr指针
auto_ptr是C++标准库提供的类模板,头文件<memory>,auto_ptr对象通过初始化指向由new创建的动态内存,它是这块内存的拥有者,一块内存不能同一时候被分给两个拥有者.当 ...
- 【C++智能指针 auto_ptr】
<More Effective C++>ITEM M9他提到auto_ptr.说是当异常产生的时候.怎么释放为对象分配的堆内存,避免反复编写内存释放语句. PS:这里书里面提到函数退出问题 ...
随机推荐
- I - Navigation Nightmare-poj 1984
约翰和他的邻居生活在一个村庄里,他们的道路修建的很特别,都是正东正西或者正南正北,但是呢他们用一种方式描述他们和邻居的位置,比如说 6号 在1号 东面13处,那么我们就可以计算出来这两家的曼哈顿距离, ...
- java并发ThreadLocal
ThreadLocal 实际就是一个map,一个线程对应一个local对象,线程创建时候,threadlocal随着创建,线程死亡ThreadLocal对象随着消失. runnable可以共享数据,要 ...
- I have a dream
1.金斧子 2.有利网 3.金融街
- Firefox中firebug和xpath checker工具的使用
一直想把自己这段时间做的东西整理下,确迟迟没有动手,现在信息抽取工作已经做的差不多,把自己感觉很好用的两个工具介绍给大家吧! Firefox真是一个好东西,它许多插件.本人是很讨厌插件的,每次电 ...
- SKShapeNode类
继承自 SKNode:UIResponder:NSObject 符合 NSCoding(SKNode)NSCopying(SKNode)NSObject(NSObject) 框架 /System/L ...
- Android打包常见错误之Export aborted because fatal lint errors were found
打包时报如下错误: <ignore_js_op> Export aborted because fatal lint errors were found. These are listed ...
- 使用EMOJI表情
因为IOS系统支持日文中的字块编码,所以在UILable,UITextField,UIAlertView等控件中使用emoji表情编码(emoji就是表情符号:词义来自日语(えもじ,e-moji,mo ...
- [Docker] Docker Client in Action
Pull the docker image: docker pull hello-world Show all the images: docker images Remove the image: ...
- C#获取文件夹下指定格式的所有文件
C#获取文件夹下指定格式的所有文件的方法,虽然很简单,但还是分享一下吧,用到时可以稍加修改和优化就可以使用. 获取指定目录下所有文件 //最要使用 System.IO.Directory.GetFil ...
- Android文档资源大放送 感兴趣的话可以网盘下载(个人收集)
Google.Android.SDK开发范例大全.第3版源码.rar http://pan.baidu.com/s/1c0epYzm 精通Android 3中文版(Pro Android 3).pdf ...