-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmartPointerImplementation.cpp
75 lines (63 loc) · 1.07 KB
/
SmartPointerImplementation.cpp
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
#include <iostream>
using namespace std;
template <class T>
class smartPtr
{
T* ptr;
public:
// So that implicit type conversions won't happen
explicit smartPtr(T* p = 0) : ptr(p)
{
cout << "In Smart A's Constructor" << endl;
}
~smartPtr()
{
cout << "In Smart A's Destructor" << endl;
delete ptr;
}
T& operator*()
{
return *ptr;
}
T* operator->()
{
return ptr;
}
void func1()
{
cout << "In Smart A's func1" << endl;
}
};
class A
{
public:
A()
{
cout << "In A's Constructor" << endl;
}
~A()
{
cout << "In A's Destructor" << endl;
}
void func1()
{
cout << "In A's func1" << endl;
}
};
int main()
{
// Normal Pointer
{
cout << endl << "Default Pointer" << endl;
A* ob = new A();
ob->func1();
delete ob;
}
// Smart Pointer
{
cout << endl << "Smart Pointer" << endl;
smartPtr <A> ob(new A);
ob->func1();
}
return 0;
}