-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_ptr.cpp
177 lines (158 loc) · 3.24 KB
/
auto_ptr.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <vld.h>
#include <assert.h>
using namespace std;
using namespace boost;
namespace C98 {
/// 原始智能指针
template <class _Ty>
class auto_ptr {
public:
auto_ptr(_Ty* p = 0) : _P(p), _Owns(p != 0)
{}
auto_ptr(const auto_ptr<_Ty>& ap):_Owns(ap._Owns),_P(ap.release())
{}
~auto_ptr() {
if (_Owns)
delete _P;
}
_Ty& operator*() { return (*get()); }
_Ty* operator->() { return get(); }
auto_ptr<_Ty>& operator=(const auto_ptr<_Ty>& ap) {
if (this != &ap) {//不是自己给自己赋值
if (_P != ap.get()) {//管理的不是同一块空间
if (_Owns) //拥有管理权,释放原空间
delete _P;
_Owns = ap._Owns;//管理权更新
}
else if (ap._Owns)//管理的是同一块空间但ap拥有管理权
_Owns = true;//管理权更新
_P = ap.release();//空间移交
}
return *this;
}
private:
_Ty* get()const { return( _P); }
_Ty* release()const {
((auto_ptr<_Ty>*)this)->_Owns = false;
return _P;
}
private:
bool _Owns;//拥有权
_Ty* _P;//管理空间
};
}
namespace VS {
/// VS版本下的智能指针,彻底转移
template<class _Ty>
class auto_ptr {
public:
typedef auto_ptr<_Ty> _Myt;
public:
auto_ptr(_Ty* p):_ptr(p)
{}
~auto_ptr() {
delete _ptr;
}
auto_ptr(_Myt& _Right): _ptr(_Right.release())
{}
_Myt& operator=(_Myt& _Right){
reset(_Right.release());
return (*this);
}
_Ty& operator*()const { return (*get()); }
_Ty* operator->()const { return (get()); }
private:
_Ty* get() { return _ptr; }
_Ty* release() {
_Ty* tmp = _ptr;
_ptr = 0;
return (tmp);
}
void reset(_Ty* pt = 0) {
if (pt != _ptr)
delete _ptr;
_ptr = pt;
}
private:
_Ty* _ptr;
};
};
namespace MyBoost {
template<class T> class scoped_ptr {
private:
T* px;
scoped_ptr(scoped_ptr const&);
scoped_ptr& operator=(scoped_ptr const&);
typedef scoped_ptr<T> this_type;
void operator==(scoped_ptr const&) const;
void operator!=(scoped_ptr const&) const;
public:
typedef T element_type;
explicit scoped_ptr(T* p = 0) : px(p)
{}
explicit scoped_ptr(std::auto_ptr<T> p) : px(p.release()) {}
~scoped_ptr() {
release(px);
}
void reset(T* p = 0) {
this_type(p).swap(*this);
}
T& operator*() const {
return *px;
}
T* operator->() const {
return px;
}
T* get() const {
return px;
}
void swap(scoped_ptr& b) {
T* tmp = b.px;
b.px = px;
px = tmp;
}
void release(T* p) {
delete p;
}
};
template<class T>
class scoped_array {
private:
T* px;
scoped_array(scoped_array const&);
scoped_array& operator=(scoped_array const&);
typedef scoped_array<T> this_type;
void operator==(scoped_array const&) const;
void operator!=(scoped_array const&) const;
public:
typedef T element_type;
explicit scoped_array(T* p = 0) : px(p) // never throws
{}
~scoped_array(){
delete[] px;
}
void reset(T* p = 0){
this_type(p).swap(*this);
}
T& operator[](std::ptrdiff_t i) const {
assert(px != 0);
assert(i >= 0);
return px[i];
}
T* get() const { return px; }
void swap(scoped_array& b) {
T* tmp = b.px;
b.px = px;
px = tmp;
}
};
}
int main() {
int* p = new int[10]{1,2,3,4,5,6,7,8,9,10};
MyBoost::scoped_array<int> ps(p);
for (int i = 0; i < 10; ++i)
cout << ps[i] << " ";
cout << endl;
return 0;
}