请问什么是auto pointer(自动指针)?

nevergrief 2007-11-27 06:47:57
请问什么是auto_ptr(自动指针),一般用来干什么呢?为什么会存在这个东西?与smart pointer有什么区别?体现了什么编程思想呢?有谁平时用过吗?非踌感谢各位大家指教啊!!!

在wiki上搜到一个简单的解释。
http://en.wikipedia.org/wiki/Auto_ptr
...全文
708 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ryfdizuo 2007-11-27
  • 打赏
  • 举报
回复
vc6的说明:

auto_ptr
template<class T>
class auto_ptr {
public:
typedef T element_type;
explicit auto_ptr(T *p = 0) throw();
auto_ptr(const auto_ptr<T>& rhs) throw();
auto_ptr<T>& operator=(auto_ptr<T>& rhs) throw();
~auto_ptr();
T& operator*() const throw();
T *operator->() const throw();
T *get() const throw();
T *release() const throw();
};

The class describes an object that stores a pointer to an allocated object of type T. The stored pointer must either be null or designate an object allocated by a new expression. The object also stores an ownership indicator. An object constructed with a non-null pointer owns the pointer. It transfers ownership if its stored value is assigned to another object. The destructor for auto_ptr<T> deletes the allocated object if it owns it. Hence, an object of class auto_ptr<T> ensures that an allocated object is automatically deleted when control leaves a block, even via a thrown exception.
pptor 2007-11-27
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20070329/16/89a3640f-c700-4e46-b74f-3323025315b2.html?seed=956194186
xlbdan 2007-11-27
  • 打赏
  • 举报
回复
智能指针是一种设计模式,也就是一个具有自动管理动态分配的内存的功能的类.

而auto_ptr是C++ STL里提供的一个智能指针的范例.
ckt 2007-11-27
  • 打赏
  • 举报
回复
智能的东西哈
让你使用指针更安全,不用去考虑释放等问题,它帮你完成
http://stdcpp.cn/read.php?wid=270
飞哥 2007-11-27
  • 打赏
  • 举报
回复
所谓智能指针,就是不用怎么考虑内存管理的指针
l_clove 2007-11-27
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20070329/16/89a3640f-c700-4e46-b74f-3323025315b2.html,佩服不已,至今没能完全领悟。
jixingzhong 2007-11-27
  • 打赏
  • 举报
回复
auto_ptr 就是智能指针

C/C++ 指针提供了足够的灵活性,也带来了无数的潜在威胁。封装一个智能指针,可以很大程度化解指针隐患
独孤过儿 2007-11-27
  • 打赏
  • 举报
回复
看这里,这个不是一句两句话能说清楚的,涉及到设计模式和编程思想的很多东西,我怕自己说不全面,误人子弟

http://topic.csdn.net/t/20051217/16/4465161.html
playing0816 2007-11-27
  • 打赏
  • 举报
回复
智能指针就是封装指针,用来自动释放指针,而不用用户去调用delete操作了。
本质就是用一个对象而非指针来保存一个指针,而这个对象会自动析构,当这个
对象自动析构的时候,其析构函数会释放它保存的指针,而不用用户去调用delete操作
释放这个指针了,而且这个对象在进行赋值或者构造函数的时候已经考虑了这个保存的
指针的所有权转移等。

64,631

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧