-
Notifications
You must be signed in to change notification settings - Fork 2
/
CustomPtr.h
83 lines (64 loc) · 1.54 KB
/
CustomPtr.h
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
//
// Created by 菅瑞霖 on 2021/4/9.
//
#ifndef NDK_DEMO_CUSTOMPTR_H
#define NDK_DEMO_CUSTOMPTR_H
#pragma once
#include <iostream>
using namespace std;
//TODO 手写智能指针
template<typename T>
class Ptr {
private:
T *object;//用于智能指针指向管理的对象
int *count;//引用计数
public:
Ptr() {
count = new int(1);
object = 0;
}
Ptr(T *t) : object(t) {
count = new int(1);
}
//析构函数
~Ptr() {
//引用计数减1,为0表示可以释放object了
if (--(*count) == 0) {
if (object) {
delete object;
}
//归零
delete count;
object = 0;
count = 0;
}
}
//拷贝构造函数
Ptr(const Ptr<T> &ptr) {
cout << "拷贝构造函数" << endl;
// 这个ptr 相当于 shared_ptr<Cat> sharedPtr2 = sharedPtr; 中的sharedPtr
++(*ptr.count);
object = ptr.object;
count = ptr.count;
}
// 自定义=号运算符重载
Ptr<T> &operator=(const Ptr<T> &ptr) {
cout << "=号运算符重载" << endl;
++(*ptr.count);
if (--(*count) == 0) {
if (object) {
delete object;
}
//归零
delete count;
object = 0;
}
object = ptr.object;
count = ptr.count;
return *this;//运算符重载的返回
}
int use_count() {
return *this->count;
}
};
#endif //NDK_DEMO_CUSTOMPTR_H