-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_shared_ptr.cpp
More file actions
73 lines (71 loc) · 1.37 KB
/
Copy pathmy_shared_ptr.cpp
File metadata and controls
73 lines (71 loc) · 1.37 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
#include <iostream>
template<typename T>
class my_shared_ptr final{
int *counter;
T *data = nullptr;
public:
my_shared_ptr() = default;
my_shared_ptr(T d) :data(new T(d)) {
counter = new int(1);
}
my_shared_ptr(const my_shared_ptr &a){
if (counter != nullptr) {
(*counter)--;
clear();
}
(*a.counter)++;
data = new T(*a.data);
counter = a.counter;
}
~my_shared_ptr() {
(*counter)--;
clear();
}
void clear() {
if (*counter == 0) {
delete counter;
delete data;
counter = nullptr;
data = nullptr;
}
}
my_shared_ptr<T> &operator=(const my_shared_ptr<T> &a) {
if (data!=a.data)
{
(*counter)--;
clear();
(*a.counter)++;
data = a.data;
counter = a.counter;
}
return *this;
}
my_shared_ptr reset(T *new_data) {
*counter--;
clear();
data = new T(*new_data);
counter = new int(1);
}
int use_count() {
return *counter;
}
T operator*() {
return *data;
}
};
template<typename T>
my_shared_ptr<T> make_shared(T data) {
my_shared_ptr<T>a(data);
return a;
}
int main()
{
my_shared_ptr<int>a = make_shared<int>(1);
std::cout << a.use_count() << std::endl;
my_shared_ptr<int>b(2);
std::cout << b.use_count() << std::endl;
b = a;
std::cout << a.use_count() << std::endl;
std::cout << b.use_count() << std::endl;
std::cout << *a << std::endl;
}