-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart_pointer_sp_impl.cpp
More file actions
112 lines (95 loc) · 2.55 KB
/
smart_pointer_sp_impl.cpp
File metadata and controls
112 lines (95 loc) · 2.55 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
//source : https://www.codeproject.com/Articles/15351/Implementing-a-simple-smart-pointer-in-c
class Person {
public:
Person() : age(0), pname(nullptr){}
Person(int age, char *name) : age(age), pname(name) {}
~Person() {
}
void display() {
std::cout << "Let's display it : " << pname << " " << age << "\n";
}
void shout() {
std::cout << "shooooooooooooooooooooout!" << "\n";
}
private:
int age;
char *pname;
};
/*
We will maintain a pointer to class RC in our SP class and
this pointer will be shared for all instances of
the smart pointer which refers to the same pointer.
*/
class RC {
public:
void addRef() {
count++;
}
int Release() {
return --count;
}
public:
int count;
};
// Implementing a reference counting
// We should not delete the Person class pointer unless no body is using it
template<typename T>
class SP {
public:
SP() : pData(0), reference(0) {
reference = new RC();
reference->addRef();
}
SP(T *pvalue) : pData(pvalue) {
reference = new RC();
reference->addRef();
}
~SP() {
if(reference->Release() == 0) {
delete pData;
delete reference;
}
}
//the smart pointer should behave like a pointer,
//it should support the same interface as pointers do.
Person& operator*() { return *pData; }
Person* operator->() { return pData; }
//we need to have an assignment operator and copy constructor
SP(const SP<T>& sp) : pData(sp.pData), reference(sp.reference) {
pData = sp.pData;
reference = sp.reference;
reference->addRef();
}
//Assignment operator
SP<T>& operator=(const SP<T>& sp) {
if(this != &sp) {
if(reference->Release() == 0) {
delete pData;
delete reference;
}
pData = sp.pData;
reference = sp.reference;
reference->addRef();
}
return *this;
}
private:
T *pData;
RC *reference;
};
//test case
int main(int argc, char **argv) {
SP<Person> p(new Person( 25, "Scott"));
p->display();
{
SP<Person> q = p;
q->display();
// Destructor of q will be called here..
SP<Person> r;
r = p;
r->display();
// Destructor of r will be called here..
}
p->display();
return 0;
}