auto_ptr动态分配对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
template < class T>
class auto_ptr
{
private :
T*ap;
public :
//constructor & destructor-----------------------------------(1)
explicit auto_ptr(T*ptr=0) throw ():ap(ptr)
{
}
~auto_ptr() throw ()
{
delete ap;
}
//Copy & assignment--------------------------------------------(2)
auto_ptr(auto_ptr& rhs) throw ():ap(rhs.release())
{
}
template < class Y>
auto_ptr(auto_ptr<Y>&rhs) throw ():ap(rhs.release())
{
}
auto_ptr& operator=(auto_ptr&rhs) throw ()
{
reset(rhs.release());
return * this ;
}
template < class Y>
auto_ptr& operator=(auto_ptr<Y>&rhs) throw ()
{
reset(rhs.release());
return * this ;
}
//Dereference----------------------------------------------------(3)
T& operator*() const throw ()
{
return *ap;
}
T* operator->() const throw ()
{
returnap;
}
//Helper functions------------------------------------------------(4)
//value access
T* get() const throw ()
{
returnap;
}
//release owner ship
T* release() throw ()
{
T*tmp(ap);
ap=0;
return tmp;
}
//reset value
void reset(T*ptr=0) throw ()
{
if (ap!=ptr)
{
deleteap;
ap=ptr;
}
}
//Special conversions-----------------------------------------------(5)
template < class Y>
struct auto_ptr_ref
{
Y*yp;
auto_ptr_ref(Y*rhs):yp(rhs){}
};
auto_ptr(auto_ptr_ref<T>rhs) throw ():ap(rhs.yp)
{
}
auto_ptr& operator=(auto_ptr_ref<T>rhs) throw ()
{
reset(rhs.yp);
return * this ;
}
template < class Y>
operator auto_ptr_ref<Y>() throw ()
{
returnauto_ptr_ref<Y>(release());
}
template < class Y>
operator auto_ptr<Y>() throw ()
{
returnauto_ptr<Y>(release());
}
};
|
作用
编辑
1
2
|
int *p= new int (0);
auto_ptr< int > ap(p);
|
1
2
3
|
int *p= new int (0);
auto_ptr< int >ap1(p);
auto_ptr< int >ap2(p);
|
1
2
|
int *pa= new int [10];
auto_ptr< int >ap(pa);
|
1
2
3
4
|
~auto_ptr() throw ()
{
if (ap) delete ap;
}
|
1
2
3
4
|
int *p= new int (0);
auto_ptr< int >ap1(p);
auto_ptr< int >ap2=ap1;
cout<<*ap1; //错误,此时ap1只剩一个null指针在手了
|
1
2
3
4
5
6
7
8
|
void f(auto_ptr< int >ap)
{
cout<<*ap;
}
auto_ptr< int >ap1( new int (0));
f(ap1);
cout<<*ap1; //错误,经过f(ap1)函数调用,ap1已经不再拥有任何对象了。
|
1
2
|
class base{};
class derived: public base{};
|
1
|
auto_ptr<base>apbase=auto_ptr<derived>( new derived);
|
1
2
3
4
5
6
|
A a1;
A a2(a1);
A a3;
a3=a1;
//那么
a2==a1,a3==a1
|
1
2
3
4
5
6
7
8
|
struct A
{
void f();
}
auto_ptr<A>apa( new A);
(*apa).f();
apa->f();
|
1
|
auto_ptr< int >ap1=auto_ptr< int >( new int (0));
|
auto_ptr动态分配对象相关推荐
- C++智能指针剖析(上)std::auto_ptr与boost::scoped_ptr
1. 引入 C++语言中的动态内存分配没有自动回收机制,动态开辟的空间需要用户自己来维护,在出函数作用域或者程序正常退出前必须释放掉. 即程序员每次 new 出来的内存都要手动 delete,否则会造 ...
- 智能指针——auto_ptr
1. 开篇 C++里面的四个智能指针:auto_ptr.unique_ptr.shared_ptr.weak_ptr,其中后三个是C++11支持,而这个auto_ptr已经被C++11弃用.但auto ...
- 关于std:auto_ptr
很多人听说过标准auto_ptr智能指针机制,但并不是每个人都天天使用它.这真是个遗憾,因为auto_ptr优雅地解决了C++设计和编码中常见的问题,正确地使用它可以生成健壮的代码.本文阐述了如何正确 ...
- Effective C++ 条款13 以对象管理资源
// // main.cpp // 条款13:以对象管理资源 // // Created by 于磊 on 2018/7/8. // Copyright © 2018 于磊. All rights r ...
- C++的智能指针auto_ptr、unique_ptr源码解析
C++的智能指针auto_ptr.unique_ptr源码解析 1.前言 2.源码准备 3.源码解析 3.1.auto_ptr解析 3.2.unique_ptr解 3.3.unique_ptr的一个偏 ...
- 如何用auto_ptr做为函数的参数进行传递
auto_ptr跟指针的原理是一样的,只不过在使用会自动释放内存.但是在函数中作为auto_ptr参数使用时,一定要注意控制权的转移问题. void testptrAddNumber(MCMDTLog ...
- auto_ptr使用总结
(1) auto_ptr的意义 auto_ptr是一种智能指针,当系统异常退出的时候避免资源泄漏(内存). 其他的资源还对应其他的智能指针. (2) auto_ptr的使用 std::auto_ptr ...
- 复习笔记(三)——C++类和对象
目录 类和对象在内存中的关系 this指针 类的静态成员 静态数据成员 静态成员函数 指针与对象 对象参数的传递 const 对象 const 数据成员 const 成员函数 存取权限的补充 muta ...
- C++独孤九剑第五式——人生几何(对象复制控制)
对酒当歌,人生几何? 譬如朝露,去日苦多. 人的一生可能惊涛骇浪,更可能波澜不惊,这次我们就来探讨一下"对象"(当然各位同学自己的对象不在本次讨论范围之内O(∩_∩)O,课后自己讨 ...
最新文章
- 用Zend Stuido 的WSDL编辑器
- jQuery 效果 - animate() 方法
- DrawerLayout侧滑
- Loadrunner中socket协议中的三个关联函数
- C#赋值运算符及解析
- 结点重要性与SIR模型基础代码
- JAVA面试题之经典题型
- 水上运动鞋行业调研报告 - 市场现状分析与发展前景预测
- 年末阿里百度等大厂技术面试题汇总,完整版开放下载
- 数据结构设计_数据结构算法设计题学起来很困难怎么破
- iStack与CSS配置实例
- Python转换图片格式 -- PIL库的使用
- java常用开发工具大合集
- ios-获取相册相机图片
- 鬼泣模仿秀01——Unity3D实现类似鬼泣的蓄力攻击(C#)
- java 排名算法_排行榜的算法
- Vue商城——首页功能
- Zynga欲收购风靡全球的画画猜字游戏Draw Something
- 全球5G手机最新排名!
- 你知道管理的精髓是什么吗?-进度猫带你走进管理者的世界