-
Notifications
You must be signed in to change notification settings - Fork 0
/
uniquePointer.cpp
107 lines (87 loc) · 2.93 KB
/
uniquePointer.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
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
#include <iostream>
#include <string>
using namespace std;
template <typename T>
class UniquePointer {
private:
T* ptr;
public:
// Constructor
explicit UniquePointer(T* p = nullptr) : ptr(p) {
cout<<"UniquePointer instance created, Contructor is called!!\n";
}
// Destructor
~UniquePointer() {
cout<<"UniquePointer deleted, Destructor is called!!\n";
delete ptr;
}
// Disable copy constructor and copy assignment (no copying allowed)
UniquePointer(const UniquePointer&) = delete;
UniquePointer& operator=(const UniquePointer&) = delete;
// Move constructor: transfers ownership from another UniquePointer
UniquePointer(UniquePointer&& other) noexcept : ptr(other.ptr) {
cout<<"Move constructor called\n";
other.ptr = nullptr; // Set the other pointer to nullptr after the move
}
// Move assignment: transfers ownership from another UniquePointer
UniquePointer& operator=(UniquePointer&& other) noexcept {
if (this != &other) { // Prevent self-assignment
delete ptr; // Free the current resource
ptr = other.ptr; // Transfer ownership
other.ptr = nullptr; // Set the other pointer to nullptr
}
cout<<"Move assignment operator called\n";
return *this;
}
// Overload dereference operator (*)
T& operator*() const {
cout<<"overloading dereference operator\n";
return *ptr;
}
// Overload arrow operator (->)
T* operator->() const {
cout<<"overloading arrow oprator\n";
return ptr;
}
// Utility function to release ownership and return the pointer
T* release() {
cout<<"utility function to release ownership\n";
T* oldPtr = ptr;
ptr = nullptr;
return oldPtr;
}
// Utility function to reset the pointer with a new one
void reset(T* p = nullptr) {
cout<<"utility function to reset the pointer with a new one\n";
delete ptr; // Free the current resource
ptr = p; // Take ownership of the new pointer
}
};
// A sample class to use with UniquePointer
class MyClass {
public:
MyClass() {
cout << "MyClass constructor called!" << '\n';
}
~MyClass() {
cout << "MyClass destructor called!" << '\n';
}
void display() const {
cout << "Hello from MyClass!" << '\n';
}
};
int main() {
// Create a UniquePointer to MyClass
UniquePointer<MyClass> unique_ptr(new MyClass());
// Use arrow operator to call display function of MyClass
unique_ptr->display();
// Transfer ownership to another UniquePointer
UniquePointer<MyClass> unique_ptr2 = move(unique_ptr);
// uptr no longer holds ownership, uptr2 now owns the resource
if (!unique_ptr.release()) {
cout << "Ownership transferred" << '\n';
}
// Use the new owner to access the object
unique_ptr2->display();
return 0;
}