Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <cstddef>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. class UniquePtr {
  7. private:
  8.     T* obj;
  9.  
  10. public:
  11.     // +
  12.     explicit UniquePtr() {
  13.         obj = nullptr;
  14.     }
  15.     // +
  16.     explicit UniquePtr(T* link): obj(link) {
  17.         link = nullptr;
  18.     }
  19.     // +
  20.     UniquePtr(UniquePtr&& a) {
  21.         obj = a.release();
  22.     }
  23.     // +
  24.     UniquePtr& operator=(nullptr_t) {
  25.         obj = nullptr;
  26.         return *this;
  27.     }
  28.     // +
  29.     UniquePtr& operator=(UniquePtr&& a) {
  30.         obj = a.release();
  31.         return *this;
  32.     }
  33.     // +
  34.     T& operator* () {
  35.         return *obj;
  36.     }
  37.     // +
  38.     T& operator* () const {
  39.         return *obj;
  40.     }
  41.     // +
  42.     T* operator-> () {
  43.         return obj;
  44.     }
  45.     // +
  46.     T* operator-> () const {
  47.         return obj;
  48.     }
  49.     // +
  50.     T* release() {
  51.         T* timeObj = obj;
  52.         obj = nullptr;
  53.         return timeObj;
  54.     }
  55.     // +
  56.     void reset(T* ptr) {
  57.         obj = ptr;
  58.         ptr = nullptr;
  59.     }
  60.     // +
  61.     void swap(UniquePtr& other) {
  62.         T* timeObj = obj;
  63.         obj = other.get();
  64.         other.reset(timeObj);
  65.     }
  66.     // +
  67.     T* get() const {
  68.         return obj;
  69.     }
  70.     // +
  71.     explicit operator bool() {
  72.         return (obj != 0);
  73.     }
  74.     // +
  75.     ~UniquePtr() {
  76.         delete obj;
  77.     }
  78. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement