智能指针_通用工具_STL
来源:互联网 发布:mac系统好还是windows 编辑:程序博客网 时间:2024/05/17 04:34
C++ STL提供的auto_ptr是一种智能型指针(smart pointer),帮助程序员防止“被异常抛出时发生资源泄露”。一下就auto_ptr的设计动机、auto_ptr的拥有权转移和auto_ptr的运用方面进行阐述。
一,auto_ptr的设计动机
1,获取一些资源。
2,执行一些动作。
3,释放所获取的资源
一个典型的例子是使用new和delete来产生和摧毁对象:
void f(){ ClassA* ptr=new ClassA; ... delete ptr;}
然而经常会忘掉delete动作,特别是当函数中存在return时更是如此。更隐晦的是当异常发生时,函数将立刻退离而调用不到尾端的delete语句。如:
void f(){ ClassA* ptr=new ClassA; try{ ....} catch(...){ delete ptr; throw; } delete ptr;}
auto_ptr是这样一种指针:它是“它所指向对象”的拥有者(owner)。所以当身为对象拥有者的auto_ptr被摧毁时,该对象也将被摧毁。auto_ptr要求一个对象智能有一个拥有者,严禁一物二主。
下面是上例改写后的版本:
//header file for auto_ptr#include <memory>void f(){ //create and initialize an auto_ptr std::auto_ptr<ClassA> ptr(new ClassA); ... //perform some operations}
二,auto_ptr拥有权(ownership)的转移
auto_ptr的copy构造函数和assignment操作符将对象拥有权交出去。如:
//initialize an auto_ptr with a new objectstd::auto_ptr<ClassA> ptr1(new ClassA);//copy the auto_ptr//-transfers ownership from ptr1 to ptr2std::auto_ptr<ClassA> ptr2(ptr1);
std::auto_ptr<ClassA> ptr1(new ClassA);std::auto_ptr<ClassA> ptr2;ptr2=ptr1; //assign the auto_ptr transfers ownership from ptr1 to ptr2
三, auto_ptr的缺陷
看例子:
//this is a bad exampletemplate <class T>void bad_print(std::auto_ptr<T> p) //p gets the ownership of passed argument{ //does p own an object? if(p.get()==NULL){ std::cout<<"NULL"; } else{ std::cout<<*p; }} //Oops, existing deletes the objects to witch p regers
只要有一个auto_ptr被当做参数,放进这个bad_ptr() 函数,它所拥有的对象(如果有的话)就一定会被删除。因为作为参数的auto_ptr会将拥有权转交给参数p,而当函数退出时,会删除p所拥有的对象。这恐怕不是程序员所希望的,最终会引起致命的执行期错误:
std::auto_ptr<int> p(new int);*p=42; //change value to which p refersbad_print(p); //Oops,deletes the memory to which p refers*p=18; //runtime error考虑到auto_ptr 的概念,我们可以用constant reference,向函数传递拥有权。事实上,constant reference 无法交出拥有权,无法变更constant reference的拥有权。
const std::auto_ptr<int> p(new int);*p=42; //change value to which p regersbad_print(p); //compile-time error*p=18; //Ok这一方案使得auto_ptr变得安全些。很多接口在需要内部拷贝时,都通过constant reference 获得原值。事实上,C++ STL的所有容器都如此,大致像这样:
template <class T>void container::insert(const T& value){ ..... x=value; .....}
其实,在这里关键字const 并非意味着你不能更改auto_ptr所拥有的对象,而是说明你不能更改auto_ptr的拥有者。例如:
如果使用const auto_ptr作为参数,对新对象的任何赋值都将导致编译器错误。就常数特性而言,const auto_ptr比较类似常数指针(T* const p),而非指向常数的指针(const T* p)---------尽管其语法上看上去比较向后者。std:auto_ptr<int> f(){const std::auto_ptr<int> p(new int); //no ownership transfer possiblestd::auto_ptr<int> q(new int); //ownership transfer possible*p=42; //OK, change value to which p refersbad_print(p); //compile time error*p=*q; //OK, change value to which p refersp=q; //compile time errorreturn p;}
参考:The C++ Standard Library (A Tutorial and Reference)
0 0
- 智能指针_通用工具_STL
- 头文件<cstddef>和<cstdlib>_通用工具_STL
- 通用工具auto_ptr 模板智能指针
- 九、 通用工具 ----smart Pointer(智能指针)---shared_ptr 指针
- 九、 通用工具 ----smart Pointer(智能指针)---weak_ptr
- 九、 通用工具 ----smart Pointer(智能指针)---unique_ptr
- C++_智能指针
- C++11 通用智能指针
- 智能指针_百度百科
- _STL使用技巧_(不定时更新)
- 辅助函数_通用工具__STL
- C++ Primer笔记8_动态内存_智能指针
- C++11 理解 (二十三) 之 通用智能指针
- 进阶篇_STL中通用算法处理数据
- C++:Boost库智能指针_删除器
- 智能指针
- 智能指针
- 智能指针
- BFS PKU 3278
- 好久 ---献给为好久苦恼的人儿
- linux rsync同步定时任务操作
- 安卓 Activity中获取ListView子View中控件
- Android WebView 因重定向无法正常goBack()解决方案
- 智能指针_通用工具_STL
- linux等待队列wait_queue_head_t和wait_queue_t
- HTML5全球普及将加速 或终结iOS与安卓界限
- 开源 Java 中文分词器 Ansj 作者孙健专访
- 分众传媒CEO江南春:没钱是这个社会进步最大的动力
- 总有这样的时候
- Android开发平台Android Studio学习之一(下载及安装)
- Struts2 valuestrack 原理
- JS学习