Advertisement
elvman

Untitled

Dec 24th, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. template<class T>
  2. struct AutoPtr
  3. {
  4.     typedef T subtype;
  5.     T* item;
  6.    
  7.     AutoPtr() : item(nullptr) { }
  8.     AutoPtr(T* v) : item(v) { if (item) item->retain(); }
  9.     AutoPtr(const AutoPtr& other): item(other.item) { if (item) item->retain(); }
  10.     ~AutoPtr() { if (item) { item->release(); item = nullptr; } }
  11.     AutoPtr& operator = (const AutoPtr& other) { if (item) item->release(); item = other.item; if (item) item->retain(); return *this; }
  12.    
  13.     AutoPtr(AutoPtr&& other): item(other.item) { other.item = nullptr; }
  14.     AutoPtr& operator = (AutoPtr&& other) { if (this != &other) { if (item) item->release(); item = other.item; other.item = nullptr; } return *this; }
  15.    
  16.     T* operator -> () const { return item; }
  17.     T& operator * () const { return *item; }
  18.     operator T* () const { return item; }
  19. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement