-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart_ptr.cpp
More file actions
191 lines (182 loc) · 4.13 KB
/
smart_ptr.cpp
File metadata and controls
191 lines (182 loc) · 4.13 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
using namespace std;
class A{
public:
void get(){
cout<<"the val of this class is:"<<val<<endl;
}
virtual ~A()=default;
private:
int val = 1;
};
class B:public A{
public:
void getb() {
cout << "the val of this class is:" << bval << endl;
}
private:
int bval = 2;
};
template <typename T>
class my_shared_ptr{
public:
/*
* 构造函数
*/
explicit my_shared_ptr(T* ptr = nullptr): _ptr(ptr){
cout<<"alloc ptr."<<endl;
}
/*
* 拷贝构造函数
*/
my_shared_ptr(my_shared_ptr&& other){
_ptr = other.relese();
}
template<typename U>
explicit my_shared_ptr(my_shared_ptr<U>&& other){
_ptr = other.relese();
}
my_shared_ptr& operator=(my_shared_ptr rhs){
rhs.swap(*this);
return *this;
}
~my_shared_ptr(){
delete _ptr;
cout<<"delete ptr."<<endl;
}
T* relese(){
T* ptr = _ptr;
_ptr = nullptr;
return ptr;
}
void swap(my_shared_ptr& rhs){
using std::swap;
swap(_ptr, rhs._ptr);
}
/*
* 重载运算符
*/
T& operator*() const{ return *_ptr;}
T* operator->() const{ return _ptr;}
T* get(){
return _ptr;
}
private:
T* _ptr;
};
class shared_count{
public:
shared_count() : _count(1){}
void add_account(){
++_count;
}
long reduce_count(){
return --_count;
}
long get_count() const{
return _count;
}
private:
long _count;
};
template <typename T>
class smart_ptr{
public:
template<typename U>
friend class smart_ptr;
//构造函数
explicit smart_ptr(T* ptr = nullptr): _ptr(ptr) {
if(ptr){
_shared_count = new shared_count();
}
}
//智能指针类型转换
template<typename U>
smart_ptr(const smart_ptr<U>& other, T* ptr){
_ptr = ptr;
if(_ptr){
other._shared_count->add_account();
_shared_count = other._shared_count;
}
}
//赋值函数
smart_ptr& operator=(smart_ptr rhs){
rhs.swap(*this);
return *this;
}
//拷贝构造函数
smart_ptr(const smart_ptr& other){
_ptr = other._ptr;
if(_ptr){
other._shared_count->add_account();
_shared_count = other._shared_count;
}
}
template<typename U>
smart_ptr(const smart_ptr<U>& other){
_ptr = other._ptr;
if(_ptr){
other._shared_count->add_account();
_shared_count = other._shared_count;
}
}
//移动构造函数
template<typename U>
smart_ptr(smart_ptr<U>&& other){
_ptr = other._ptr;
if(_ptr){
_shared_count = other._shared_count;
other._ptr = nullptr;
}
}
~smart_ptr(){
if(_ptr && !_shared_count->reduce_count()){
delete _ptr;
delete _shared_count;
}
}
void swap(smart_ptr& rhs){
using std::swap;
swap(_ptr, rhs._ptr);
swap(_shared_count, rhs._shared_count);
}
long use_count() const{
if(_ptr){
return _shared_count->get_count();
}else{
return 0;
}
}
/*
* 重载运算符
*/
T& operator*() const{ return *_ptr;}
T* operator->() const{ return _ptr;}
T* get() const {
return _ptr;
}
private:
T* _ptr;
shared_count* _shared_count;
};
template<typename T, typename U>
smart_ptr<T> dynamic_pointer_cast(const smart_ptr<U>& other){
T* ptr = dynamic_cast<T*>(other.get());
return smart_ptr<T>(other, ptr);
}
int main(){
// my_shared_ptr<B> a{new B};
// my_shared_ptr<A> b;
// b = a;
smart_ptr<B> ptr1{new B};
cout<<"use count of ptr1 is:"<<ptr1.use_count()<<endl;
smart_ptr<A> ptr2;
cout<<"use count of ptr2 is:"<<ptr2.use_count()<<endl;
ptr2 = ptr1;
cout<<"use count of ptr2 is:"<<ptr2.use_count()<<endl;
cout<<"use count of ptr1 is:"<<ptr1.use_count()<<endl;
smart_ptr<B> ptr3 = dynamic_pointer_cast<B>(ptr2);
cout<<"use count of ptr3 is:"<<ptr3.use_count()<<endl;
//ptr2->getb();
return 0;
}