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:这里书里面提到函数退出问题 ...
随机推荐
- 给Eclipse提速的7个技巧(转载的)
大约一个月前,我发表了一篇博客,其中介绍了对Eclipse的爱与恨. 有些人问我如何给Eclipse提速,这篇文章就来讨论这个问题.顺带提一下,这篇文章不是比较IDE,所以不要说你讨厌某个IDE而 ...
- java排序算法-插入排序
public class InsertSortUtils { public static void main(String[] args) { insertSortTest(); shellSortT ...
- 在SQL Server 中启用 FileStream
最近在研究在数据库中存储大数据文件,看到了FileStream 这个功能,记录下来以备后用 FileStream 一般在安装的时候默认是不启用的,如果你留意的话,在选择数据库文件路径那个窗口,有一个标 ...
- [Angular 2] Passing Observables into Components with Async Pipe
The components inside of your container components can easily accept Observables. You simply define ...
- CSDN挑战编程——《数学问题》
数学问题 题目详情: 给你两个长度为n的正整数序列分别为{a1,a2,a3...an},{b1,b2,b3...bn},0<ai,bi<=100: 设S=max{x1*a1+x2*a2+x ...
- WinSock IO模型 -- WSAEventSelect模型事件触发条件说明
FD_READ事件 l 调用WSAEventSelect函数时,如果当前有数据可读 l 有数据到达时,并且没有发送过FD_READ事件 l 调用recv/recvfrom函数后,仍然有数据可读时 ...
- 【转载】python:特殊函数使用方式
[转载]廖雪峰的官方网站 可变参数 在Python函数中,还可以定义可变参数.顾名思义,可变参数就是传入的参数个数是可变的. 我们以数学题为例子,给定一组数字a,b,c……,请计算a2 + b2 + ...
- NYOJ 116士兵杀敌(二) 树状数组
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=116 士兵杀敌(一) 数组是固定的,所以可以用一个sum数组来保存每个元素的和就行,但是不 ...
- css基础回顾-定位:position
w3school 对position定义和说明是: 定义和用法: position 属性规定元素的定位类型. 说明: 这个属性定义建立元素布局所用的定位机制.任何元素都可以定位,不过绝对或固定元素会生 ...
- CSS优先级总结(转载)
样式的优先级 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下: (外部样式)External styl ...