-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer.hpp
More file actions
63 lines (48 loc) · 717 Bytes
/
Copy pathpointer.hpp
File metadata and controls
63 lines (48 loc) · 717 Bytes
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
#ifndef _POINTER_H
#define _POINTER_H
template<class T>
class pointer {
public:
pointer() {
new_one();
}
pointer(T *obj) {
new_one(obj);
}
pointer(const pointer<T> &p) {
assign(p);
}
pointer<T> & operator=(pointer<T> p) {
deref();
return assign(p);
}
T * operator->() {
return _obj;
}
~pointer() {
deref();
}
private:
T *_obj;
unsigned int *_ref;
private:
void new_one(T *obj = NULL) {
_obj = obj;
_ref = new unsigned int(1);
}
void deref() {
if (--(*_ref) == 0) {
if (_obj) delete _obj;
delete _ref;
}
_obj = NULL;
_ref = NULL;
}
pointer<T> & assign(const pointer<T> &p) {
_obj = p._obj;
_ref = p._ref;
(*_ref)++;
return *this;
}
};
#endif