auto_ptr源码剖析
/*
* Copyright (c) 1997-1999
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*/ #ifndef __SGI_STL_MEMORY
#define __SGI_STL_MEMORY #include <stl_algobase.h>
#include <stl_alloc.h>
#include <stl_construct.h>
#include <stl_tempbuf.h>
#include <stl_uninitialized.h>
#include <stl_raw_storage_iter.h> __STL_BEGIN_NAMESPACE #if defined(__SGI_STL_USE_AUTO_PTR_CONVERSIONS) && \
defined(__STL_MEMBER_TEMPLATES) template<class _Tp1> struct auto_ptr_ref {
_Tp1* _M_ptr;
auto_ptr_ref(_Tp1* __p) : _M_ptr(__p) {}
}; #endif template <class _Tp> class auto_ptr {
private:
_Tp* _M_ptr; // 维护一个类型指针 public:
typedef _Tp element_type; // 构造函数,初始化所维护的指针
explicit auto_ptr(_Tp* __p = ) __STL_NOTHROW : _M_ptr(__p) {} // 拷贝构造函数,转移控制权,将__a维护的指针转交给当前对象来维护;
auto_ptr(auto_ptr& __a) __STL_NOTHROW : _M_ptr(__a.release()) {} #ifdef __STL_MEMBER_TEMPLATES
template <class _Tp1> auto_ptr(auto_ptr<_Tp1>& __a) __STL_NOTHROW
: _M_ptr(__a.release()) {}
#endif /* __STL_MEMBER_TEMPLATES */ // 赋值构造函数,重新设定控制权
auto_ptr& operator=(auto_ptr& __a) __STL_NOTHROW {
if (&__a != this) {
delete _M_ptr; // 解除当前控制权,将所维护的指针所指向的对象析构掉。
_M_ptr = __a.release(); // 重新设定控制权,将__a维护的指针转交给当前对象来维护。
}
return *this;
} #ifdef __STL_MEMBER_TEMPLATES
template <class _Tp1>
auto_ptr& operator=(auto_ptr<_Tp1>& __a) __STL_NOTHROW {
if (__a.get() != this->get()) {
delete _M_ptr;
_M_ptr = __a.release();
}
return *this;
}
#endif /* __STL_MEMBER_TEMPLATES */ // Note: The C++ standard says there is supposed to be an empty throw
// specification here, but omitting it is standard conforming. Its
// presence can be detected only if _Tp::~_Tp() throws, but (17.4.3.6/2)
// this is prohibited.
~auto_ptr() { delete _M_ptr; } // 析构函数,释放所维护指针指向的资源 _Tp& operator*() const __STL_NOTHROW {
return *_M_ptr; // 重载*操作符,返回对象引用
}
_Tp* operator->() const __STL_NOTHROW {
return _M_ptr; // 重载->操作符,返回对象地址
}
_Tp* get() const __STL_NOTHROW {
return _M_ptr; // 获取所维护指针
}
_Tp* release() __STL_NOTHROW {
_Tp* __tmp = _M_ptr;
_M_ptr = ; // 转移控制权,当前对象不再维护任何指针
return __tmp; // 让出控制权
}
// 重新设定控制权,如果以默认值调用,则释放对象,结束控制权
void reset(_Tp* __p = ) __STL_NOTHROW {
if (__p != _M_ptr) {
delete _M_ptr;
_M_ptr = __p;
}
} // According to the C++ standard, these conversions are required. Most
// present-day compilers, however, do not enforce that requirement---and,
// in fact, most present-day compilers do not support the language
// features that these conversions rely on. #if defined(__SGI_STL_USE_AUTO_PTR_CONVERSIONS) && \
defined(__STL_MEMBER_TEMPLATES) public:
auto_ptr(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW
: _M_ptr(__ref._M_ptr) {} auto_ptr& operator=(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW {
if (__ref._M_ptr != this->get()) {
delete _M_ptr;
_M_ptr = __ref._M_ptr;
}
return *this;
} template <class _Tp1> operator auto_ptr_ref<_Tp1>() __STL_NOTHROW
{ return auto_ptr_ref<_Tp1>(this->release()); }
template <class _Tp1> operator auto_ptr<_Tp1>() __STL_NOTHROW
{ return auto_ptr<_Tp1>(this->release()); } #endif /* auto ptr conversions && member templates */
}; __STL_END_NAMESPACE #endif /* __SGI_STL_MEMORY */ // Local Variables:
// mode:C++
// End:
auto_ptr源码剖析的更多相关文章
- 《STL源码剖析》相关面试题总结
原文链接:http://www.cnblogs.com/raichen/p/5817158.html 一.STL简介 STL提供六大组件,彼此可以组合套用: 容器容器就是各种数据结构,我就不多说,看看 ...
- 面试题总结(三)、《STL源码剖析》相关面试题总结
声明:本文主要探讨与STL实现相关的面试题,主要参考侯捷的<STL源码剖析>,每一个知识点讨论力求简洁,便于记忆,但讨论深度有限,如要深入研究可点击参考链接,希望对正在找工作的同学有点帮助 ...
- 智能指针分析及auto_ptr源码
简介 C++没有内存自动回收机制,对堆内存的管理就是简单的new和delete,每次new出来的内存都需要手动delete释放.但由于忘记.流程复杂或者异常退出等,都有可能导致没有执行delete释放 ...
- jQuery之Deferred源码剖析
一.前言 大约在夏季,我们谈过ES6的Promise(详见here),其实在ES6前jQuery早就有了Promise,也就是我们所知道的Deferred对象,宗旨当然也和ES6的Promise一样, ...
- Nodejs事件引擎libuv源码剖析之:高效线程池(threadpool)的实现
声明:本文为原创博文,转载请注明出处. Nodejs编程是全异步的,这就意味着我们不必每次都阻塞等待该次操作的结果,而事件完成(就绪)时会主动回调通知我们.在网络编程中,一般都是基于Reactor线程 ...
- Apache Spark源码剖析
Apache Spark源码剖析(全面系统介绍Spark源码,提供分析源码的实用技巧和合理的阅读顺序,充分了解Spark的设计思想和运行机理) 许鹏 著 ISBN 978-7-121-25420- ...
- 基于mybatis-generator-core 1.3.5项目的修订版以及源码剖析
项目简单说明 mybatis-generator,是根据数据库表.字段反向生成实体类等代码文件.我在国庆时候,没事剖析了mybatis-generator-core源码,写了相当详细的中文注释,可以去 ...
- STL"源码"剖析-重点知识总结
STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点略多 :) 1.STL概述 STL提供六大组件,彼此可以组合 ...
- SpringMVC源码剖析(四)- DispatcherServlet请求转发的实现
SpringMVC完成初始化流程之后,就进入Servlet标准生命周期的第二个阶段,即“service”阶段.在“service”阶段中,每一次Http请求到来,容器都会启动一个请求线程,通过serv ...
随机推荐
- 转:logBack.xml配置路径
http://blog.csdn.net/z69183787/article/details/30284391 http://www.cppblog.com/fwxjj/archive/2012/08 ...
- ubuntu apt-get 时 Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
sudo cp /etc/apt/sources.list ~/ sudo wget "http://pastebin.com/raw.php?i=uzhrtg5M" -O /et ...
- 一个View的子类实例化
View子类的实例化.如果是在activity中通过findViewById的形式实例化,那么它的具体的构造函数是什么呢,看看父类View的源码就容易发现是 通过这个构造函数实例化的 public V ...
- qq加好友加群限制ip怎么解决
目前各样格式的推广都会用到腾讯QQ,现在就遇到了问题.QQ加好友加群,经常会提示你的账号存在不安全因素,暂停加好友功能.这个原因都是本地同一个IP,登陆的QQ过多,加好友过多.导致这个IP被记录,相当 ...
- Python之路 day2 集合的基本操作
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa ''' #集合是无序的 集合的关系测试, 增加,删除,查找等操作 ''' #列表去重 ...
- Virtualbox安装USB2.0/3.0
系统:Ubuntu16.04 软件:Virtualbox5.1 1.打开Virtualbox,不启动虚拟系统. 2.点击设置->USB->启动usb2.0. 3.若发现不能启用,则到官网下 ...
- 自己建二维obj
经常用到啊 在项目流程管理里面用到
- 商业信息管理系统 Bizagi 建模pattern
WCP 1- Sequence This pattern is used to model dependencies between tasks so that one task cannot sta ...
- java 的 sqlHelper,改改之后也适用于不使用 EF 的 C# 项目,包含查询和建表。
这个类用来拼接 sql. package com.ly.orm; public class Query { protected Query(String v) { sql = v; } public ...
- .NET牛人需要了解的问题[转]
任何一个使用.NET的人 描述线程与进程的区别? 什么是Windows服务,它的生命周期与标准的EXE程序有什么不同 Windows上的单个进程所能访问的最大内存量是多少?它与系统的最大虚拟内存一样吗 ...