-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared_ptr_poe.cpp
More file actions
86 lines (71 loc) · 2.23 KB
/
shared_ptr_poe.cpp
File metadata and controls
86 lines (71 loc) · 2.23 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
#include <iostream>
template <typename T>
class SharedPtr {
public:
// 默认构造函数
SharedPtr(T *ptr = nullptr) : ptr_(ptr), ref_count_(new size_t(1)) {}
// 拷贝构造函数
SharedPtr(const SharedPtr &other)
: ptr_(other.ptr_), ref_count_(other.ref_count_) {
++(*ref_count_); // 增加引用计数
}
// 移动构造函数
SharedPtr(SharedPtr &&other) noexcept
: ptr_(other.ptr_), ref_count_(other.ref_count_) {
other.ptr_ = nullptr;
other.ref_count_ = nullptr;
}
// 析构函数
~SharedPtr() { release(); }
// 拷贝赋值运算符
SharedPtr &operator=(const SharedPtr &other) {
if (this != &other) {
release(); // 释放当前资源
ptr_ = other.ptr_;
ref_count_ = other.ref_count_;
++(*ref_count_); // 增加引用计数
}
return *this;
}
// 移动赋值运算符
SharedPtr &operator=(SharedPtr &&other) noexcept {
if (this != &other) {
release(); // 释放当前资源
ptr_ = other.ptr_;
ref_count_ = other.ref_count_;
other.ptr_ = nullptr;
other.ref_count_ = nullptr;
}
return *this;
}
// 重载解引用操作符
T &operator*() const { return *ptr_; }
// 重载箭头操作符
T *operator->() const { return ptr_; }
// 获取引用计数
size_t use_count() const { return *ref_count_; }
private:
T *ptr_; // 原始指针
size_t *ref_count_; // 引用计数
// 释放资源
void release() {
if (--(*ref_count_) == 0) {
delete ptr_; // 删除原始指针
delete ref_count_; // 删除引用计数
}
}
};
// 示例用法
int main() {
SharedPtr<int> sp1(new int(10));
std::cout << "Value: " << *sp1 << ", Count: " << sp1.use_count()
<< std::endl;
{
SharedPtr<int> sp2 = sp1; // 拷贝构造
std::cout << "Value: " << *sp2 << ", Count: " << sp1.use_count()
<< std::endl;
} // sp2 超出作用域,引用计数减少
std::cout << "Count after sp2 out of scope: " << sp1.use_count()
<< std::endl;
return 0;
}