C++智能指针之auto_ptr
2013-04-21 17:44
483 查看
Deprecated in C++ 11,因为Transfer of ownership。比如作为函数参数传递时,会丧失所有权。
get() 获取原始指针
release() 释放auto_ptr并返回原始指针
reset(X*) 释放对象并为auto_ptr设置新值
operator* 解引用对象
operator-> 解引用对象成员
operator= 拷贝并转移所有权
get() 获取原始指针
release() 释放auto_ptr并返回原始指针
reset(X*) 释放对象并为auto_ptr设置新值
operator* 解引用对象
operator-> 解引用对象成员
operator= 拷贝并转移所有权
#include <memory> struct S { int i; }; int main() { std::auto_ptr<int> p1(new int); *p1.get() = 1; std::auto_ptr<int> p2(p1); // p1 is now null-pointer auto_ptr std::auto_ptr<int> p3 = p2; // p2 is now null-pointer auto_ptr p3.reset(new int); *p3 = 2; int* pi = p3.release(); // p3 is now null-pointer auto_ptr delete pi; std::auto_ptr<S> ps(new S); ps->i = 3; return 0; }
相关文章推荐
- C++智能指针--auto_ptr指针
- C++ auto_ptr智能指针的用法
- C++ 智能指针auto_ptr详解
- C++ 智能指针Auto_PTR 分析http://www.cnblogs.com/skyofbitbit/p/3649776.html
- C++:智能指针_auto_ptr_vs版本
- C++智能指针之auto_ptr
- C++ 智能指针Auto_PTR 分析
- C++智能指针:auto_ptr、shared_ptr、weak_ptr等
- 【C++】智能指针 auto_ptr
- C++智能指针之auto_ptr
- C/C++:智能指针原理(运算符重载)、使用auto_ptr<A>
- C++智能指针auto_ptr和nuique_ptr
- C++ 智能指针 auto_ptr 和 shared_ptr
- c++ 模板学习笔记:类模板模拟auto_ptr智能指针(权哥)
- C++常用的智能指针auto_ptr、shared_ptr
- C++ 智能指针auto_ptr
- 【C/C++】智能指针auto_ptr,share_ptr,unique_ptr
- c++智能指针auto_ptr的实现
- C++智能指针--auto_ptr指针
- C++智能指针auto_ptr的原理及使用