scoped_ptr.h 687 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #pragma once
  2. template<typename T>
  3. class scoped_ptr
  4. {
  5. public:
  6. scoped_ptr(T* ptr);
  7. scoped_ptr<T>& operator=(T* lhs);
  8. T* operator->();
  9. bool operator==(T* lhs);
  10. ~scoped_ptr();
  11. T* operator *();
  12. private:
  13. T* p;
  14. };
  15. template<typename T>
  16. scoped_ptr<T>::scoped_ptr(T* ptr) :p(ptr)
  17. {
  18. }
  19. template<typename T>
  20. scoped_ptr<T>& scoped_ptr<T>::operator=(T* lhs)
  21. {
  22. if (p != nullptr)
  23. {
  24. delete p;
  25. p = nullptr;
  26. }
  27. p = lhs;
  28. return *this;
  29. }
  30. template<typename T>
  31. T* scoped_ptr<T>::operator->()
  32. {
  33. return p;
  34. }
  35. template<typename T>
  36. scoped_ptr<T>::~scoped_ptr()
  37. {
  38. if (p != nullptr)
  39. {
  40. delete p;
  41. p = nullptr;
  42. }
  43. }
  44. template<typename T>
  45. bool scoped_ptr<T>::operator==(T* lhs)
  46. {
  47. return p == lhs;
  48. }