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:这里书里面提到函数退出问题 ...
随机推荐
- Unity3D AssentStore 下载的package存放目录(WinXP,Win8,Mac OS X)
网上找到如下: Windows 8,C:\Users\<你的用户名>\AppData\Roaming\Unity\Asset Store Mac OS X,~/Library/Unity/ ...
- ASP.NET MVC框架开发系列课程 (webcast视频下载)
课程讲师: 赵劼 MSDN特邀讲师 赵劼(网名“老赵”.英文名“Jeffrey Zhao”,技术博客为http://jeffreyzhao.cnblogs.com),微软最有价值专家(ASP.NET ...
- 生成 JSON 数据
//build an info object and convert to json NSDictionary* info = [NSDictionary dictionaryWithObjectsA ...
- android获得屏幕高度和宽度
获取屏幕的宽度与高度有以下几种方法: .WindowManager wm = (WindowManager) getContext() .getSystemSe ...
- UI开发--响应者链条
一.触摸事件处理的详细过程 用户点击屏幕后产生的一个触摸事件,经过一些列的传递过程后,会找到最合适的视图控件来处理这个事件 找到最合适的视图控件后,就会调用控件的touches方法来作具体的事件处理 ...
- 如何完全退出android应用程序
当一个android应用程序包含多个activity时,要完全退出android应用程序,便要销毁掉所有的activity,下面是一种网上流传的比较经典完美的方法: 首先要定义一个继承Applicat ...
- hibernate01ORM的引入
/**01.之前的方式 在while()中书写的 * int id = rs.getInt("gradeid"); String gradeName = rs.getString( ...
- 数据库操作封装类 DBHelper.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Co ...
- mysql_fetch_assoc() ,mysql_fetch_array() , mysql_fetch_row()的区别
1. mysql_fetch_assoc() 函数从结果集中取得一行作为关联数组. 返回根据从结果集取得的行生成的关联数组,如果没有更多行,则返回 false. 输出: Array ( [LastNa ...
- ruby 知识点
$LOAD_PATH 执行 require 读取文件时搜索的目录名数组,也可以写作 $: 创建 URI 的时候可以直接这样 URI("http://www.dy2018.com/i/9751 ...