UniquePointer.h 885 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #ifndef UNIQUEPOINTER_H
  2. #define UNIQUEPOINTER_H
  3. template<typename T>
  4. class UniquePointer {
  5. T* t;
  6. public:
  7. UniquePointer(T* t) : t(t) {
  8. }
  9. UniquePointer() : UniquePointer(nullptr) {
  10. }
  11. ~UniquePointer() {
  12. delete t;
  13. }
  14. UniquePointer(const UniquePointer&) = delete;
  15. UniquePointer& operator=(const UniquePointer&) = delete;
  16. UniquePointer(UniquePointer&& other) : UniquePointer(other.t) {
  17. other.t = nullptr;
  18. }
  19. UniquePointer& operator=(UniquePointer&& other) {
  20. if(this != &other) {
  21. delete t;
  22. t = other.t;
  23. other.t = nullptr;
  24. }
  25. return *this;
  26. };
  27. T* operator->() {
  28. return t;
  29. }
  30. const T* operator->() const {
  31. return t;
  32. }
  33. operator T*() {
  34. return t;
  35. }
  36. operator const T*() const {
  37. return t;
  38. }
  39. };
  40. #endif