-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweak_ptr.cpp
More file actions
68 lines (57 loc) · 1.79 KB
/
weak_ptr.cpp
File metadata and controls
68 lines (57 loc) · 1.79 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
#include <iostream>
// weak_ptr又叫做弱指针,它是用来解决shared_ptr循环引用的问题。为啥叫弱呢?
// shared_ptr A被赋值给shared_ptr B时,A的引用计数加1,;shared_ptr A被赋值给weak_ptr C时,A的引用计数不变。
// weak_ptr在使用时,是与shared_ptr绑定的。基于SharedPtr实现来实现demo版的WeakPtr,并解决循环引用的问题,全部代码如下:
template <typename T>
class SharedPtr {
public:
int *counter;
int *weakref;
T *resource;
SharedPtr(T *resc = nullptr) {
cout << __PRETTY_FUNCTION__ << endl;
counter = new int(1);
weakref = new int(0);
resource = resc;
}
SharedPtr(const SharedPtr &rhs) {
cout << __PRETTY_FUNCTION__ << endl;
resource = rhs.resource;
counter = rhs.counter;
++*counter;
}
SharedPtr &operator=(const SharedPtr &rhs) {
cout << __PRETTY_FUNCTION__ << endl;
--*counter;
if (*counter == 0) {
delete resource;
}
resource = rhs.resource;
counter = rhs.counter;
++*counter;
}
~SharedPtr() {
cout << __PRETTY_FUNCTION__ << endl;
--*counter;
if (*counter == 0) {
delete counter;
delete resource;
}
}
int use_count() { return *counter; }
};
template <typename T>
class WeakPtr {
public:
T *resource;
WeakPtr(T *resc = nullptr) {
cout << __PRETTY_FUNCTION__ << endl;
resource = resc;
}
WeakPtr &operator=(SharedPtr<T> &ptr) {
cout << __PRETTY_FUNCTION__ << endl;
resource = ptr.resource;
++*ptr.weakref; // 赋值时引用计数counter不变,改变弱引用计数weakref
}
~WeakPtr() { cout << __PRETTY_FUNCTION__ << endl; }
};