forked from vindex10/codoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
54 lines (45 loc) · 1.13 KB
/
main.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
/**
* Smart pointer implementation and testing
*/
#include <iostream>
using namespace std;
/** Template class for Smart Pointer.
* Implementation of pointer which destructs itself automatically.
*
* @example
* @code
* {
* # Memory for int(10) will be deallocated before `}`
* SmartPtr<int> a(new int(10));
* }
* @endcode
*
*
*/
template <class PtrType> class SmartPtr {
private:
PtrType *ptr; /**< Storage for pointer to data */
public:
/** Initialization constructor.
* @param ptr a pointer to data
*/
SmartPtr(PtrType* ptr):ptr(ptr) {}
/** Destructor to clean data referenced by stored pointer */
~SmartPtr() {
delete ptr;
}
/** Dereference operator has to be overloaded to change stored data */
PtrType& operator*() const {
return *ptr;
}
};
/** Test implemented class SmartPtr
* Create smart pointer, print value, modify and print again.
*/
int main() {
SmartPtr<int> myPtr(new int(10));
cout << *myPtr << endl;
*myPtr = 11;
cout << *myPtr << endl;
return 0;
}