Created
December 10, 2025 15:24
-
-
Save XoLinA/9083b993a4027c74ca061e30af9ef661 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| template <class T> | |
| class my_unique_ptr | |
| { | |
| T* ptr; | |
| public: | |
| my_unique_ptr(T* ptr = nullptr) :ptr(ptr) {} | |
| my_unique_ptr(const my_unique_ptr <T>& obj) = delete; | |
| my_unique_ptr<T> operator + (my_unique_ptr<T>& obj) = delete; | |
| my_unique_ptr(my_unique_ptr<T>&& obj) | |
| { | |
| ptr = obj.ptr; | |
| obj.ptr = nullptr; | |
| } | |
| ~my_unique_ptr() | |
| { | |
| if (ptr != nullptr) | |
| delete ptr; | |
| } | |
| void Reset() | |
| { | |
| delete ptr; | |
| ptr = nullptr; | |
| } | |
| T* Get() | |
| { | |
| return ptr; | |
| } | |
| //T& operator* const | |
| //{ | |
| // return *ptr; | |
| //} | |
| T* operator-> ()const | |
| { | |
| return ptr; | |
| } | |
| }; | |
| template <class T> | |
| class my_shared_ptr | |
| { | |
| T* ptr; | |
| int* refCount; | |
| public: | |
| my_shared_ptr(T* _ptr = nullptr) | |
| { | |
| ptr = _ptr; | |
| if (ptr != nullptr) | |
| { | |
| refCount = new int(1); | |
| } | |
| else | |
| { | |
| refCount = new int(0); | |
| } | |
| } | |
| my_shared_ptr(const my_shared_ptr<T>& obj) | |
| { | |
| ptr = obj.ptr; | |
| refCount = obj.refCount; | |
| ++(*refCount); | |
| } | |
| my_shared_ptr<T>& operator=(const my_shared_ptr<T>& obj) | |
| { | |
| if (this != &obj) | |
| { | |
| Release(); | |
| ptr = obj.ptr; | |
| refCount = obj.refCount; | |
| ++(*refCount); | |
| } | |
| return *this; | |
| } | |
| my_shared_ptr(my_shared_ptr<T>&& obj) | |
| { | |
| ptr = obj.ptr; | |
| refCount = obj.refCount; | |
| obj.ptr = nullptr; | |
| obj.refCount = new int(0); | |
| } | |
| my_shared_ptr<T>& operator=(my_shared_ptr<T>&& obj) | |
| { | |
| if (this != &obj) | |
| { | |
| Release(); | |
| ptr = obj.ptr; | |
| refCount = obj.refCount; | |
| obj.ptr = nullptr; | |
| obj.refCount = new int(0); | |
| } | |
| return *this; | |
| } | |
| void Release() | |
| { | |
| if (--(*refCount) == 0) | |
| { | |
| delete ptr; | |
| delete refCount; | |
| } | |
| } | |
| ~my_shared_ptr() | |
| { | |
| Release(); | |
| } | |
| T* Get() const | |
| { | |
| return ptr; | |
| } | |
| T* operator->() const | |
| { | |
| return ptr; | |
| } | |
| int UseCount() const | |
| { | |
| return *refCount; | |
| } | |
| }; | |
| int main() | |
| { | |
| cout << "**************** my_unique_ptr *********************\n"; | |
| int* p = new int(10); | |
| { | |
| my_unique_ptr<int> ptr1(p); | |
| cout << ptr1.Get() << endl; | |
| cout << *(ptr1.Get()) << endl; | |
| } | |
| cout << p << endl; | |
| cout << "**************** my_shared_ptr *********************\n"; | |
| my_shared_ptr<int> sp1(new int(20)); | |
| cout << "Count = " << sp1.UseCount() << endl; | |
| { | |
| my_shared_ptr<int> sp2 = sp1; | |
| cout << "After copy count = " << sp1.UseCount() << endl; | |
| my_shared_ptr<int> sp3(sp1); | |
| cout << "After second copy count = " << sp1.UseCount() << endl; | |
| cout << *sp2.Get() << endl; | |
| } | |
| cout << "After scope count = " << sp1.UseCount() << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment