-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique_pointer.h
88 lines (66 loc) · 1.55 KB
/
unique_pointer.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
84
85
86
87
88
//
// Created by ali-masa on 2/18/20.
//
#ifndef SMARTPOINTEREXERCISE_UNIQUEPOINTER_H
#define SMARTPOINTEREXERCISE_UNIQUEPOINTER_H
#include <stddef.h>
template <class T>
class UniquePointer;
template <class T>
class UnownedPointer
{
template <class V>
friend class UniquePointer;
public:
T* uniquePointer;
};
template <class T>
class UniquePointer
{
friend UnownedPointer<T> move(UniquePointer<T>& uniquePointer)
{
UnownedPointer<T> unownedPointer;
unownedPointer.uniquePointer = uniquePointer.pntr;
uniquePointer.pntr = NULL;
return unownedPointer;
}
private:
T* pntr;
explicit UniquePointer(UniquePointer& AutoPointer): pntr(AutoPointer->pntr){}
public:
explicit UniquePointer(T* pntr = NULL): pntr(pntr){}
UniquePointer<T>& operator=(UnownedPointer<T> unownedPointer)
{
this->pntr = unownedPointer.uniquePointer;
return *this;
}
UniquePointer<T>& operator=(UnownedPointer<T>& unownedPointer)
{
this->pntr = unownedPointer.uniquePointer;
unownedPointer.uniquePointer = NULL;
return *this;
}
UniquePointer<T>& operator=(UniquePointer<T>& uniquePointer)
{
this->pntr = uniquePointer.pntr;
uniquePointer.pntr = NULL;
return *this;
}
~UniquePointer()
{
delete pntr;
}
T* operator ->()
{
return pntr;
}
T& operator *()
{
return *pntr;
}
T* getRowPointer()
{
return pntr;
}
};
#endif //SMARTPOINTEREXERCISE_UNIQUEPOINTER_H