Last active
September 11, 2018 12:47
-
-
Save cagbal/790d74fc9cac51795d579c6a92ce6d20 to your computer and use it in GitHub Desktop.
Just practicing, don't take it too serious: Amazingly, Suprisingly and Super Smart Pointer Practice
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 MySuperSmartPtr | |
| { | |
| public: | |
| T* ptr; | |
| MySuperSmartPtr(T* pointer=nullptr) : ptr(pointer) | |
| { | |
| } | |
| MySuperSmartPtr(MySuperSmartPtr&& smart_ptr) : ptr(smart_ptr.ptr) | |
| { | |
| smart_ptr.ptr = nullptr; | |
| } | |
| ~MySuperSmartPtr() | |
| { | |
| delete ptr; | |
| } | |
| T& operator=(MySuperSmartPtr&& sptr) | |
| { | |
| if (&sptr == this) | |
| { | |
| return *this; | |
| } | |
| delete ptr; | |
| ptr = sptr.ptr; | |
| sptr.ptr = nullptr; | |
| return *this; | |
| } | |
| T& operator*() | |
| { | |
| return *ptr; | |
| } | |
| T* operator->() | |
| { | |
| return ptr; | |
| } | |
| }; | |
| int main() | |
| { | |
| MySuperSmartPtr<int> mssp(new int(3)); | |
| cout<<*mssp; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment