Advertisement
BeloMaximka

Belov_HW_O36

Oct 22nd, 2021
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. template <class T>
  6. class my_shared_ptr
  7. {
  8.     int* count;
  9.     T* ptr;
  10. public:
  11.     my_shared_ptr();
  12.     my_shared_ptr(T* ptr);
  13.     my_shared_ptr(const my_shared_ptr& obj);
  14.     my_shared_ptr(my_shared_ptr&& obj);
  15.     ~my_shared_ptr();
  16.  
  17.     T* get()const;
  18.     int use_count()const;
  19.     bool unique()const;
  20.     T& operator=(const my_shared_ptr& obj);
  21.     T& operator*()const;
  22.     T* operator->() const;
  23.     T& operator[](int index)const;
  24.  
  25.     bool operator==(nullptr_t)const;
  26.     bool operator==(const my_shared_ptr& obj)const;
  27.     bool operator!=(const my_shared_ptr& obj)const;
  28.     bool operator<(const my_shared_ptr& obj)const;
  29.     bool operator<=(const my_shared_ptr& obj)const;
  30.     bool operator>(const my_shared_ptr& obj)const;
  31.     bool operator>=(const my_shared_ptr& obj)const;
  32.  
  33.     friend ostream& operator<<<>(ostream& os, my_shared_ptr& obj);
  34. };
  35.  
  36. template <class T>
  37. my_shared_ptr<T>::my_shared_ptr()
  38. {
  39.     ptr = nullptr;
  40.     count = nullptr;
  41. }
  42.  
  43. template <class T>
  44. my_shared_ptr<T>::my_shared_ptr(T* ptr)
  45. {
  46.     count = new int{ 1 };
  47.     this->ptr = ptr;
  48. }
  49.  
  50. template <class T>
  51. my_shared_ptr<T>::my_shared_ptr(const my_shared_ptr& obj)
  52. {
  53.     count = obj.count;
  54.     (*count)++;
  55.     ptr = obj.ptr;
  56. }
  57.  
  58. template <class T>
  59. my_shared_ptr<T>::my_shared_ptr(my_shared_ptr&& obj)
  60. {
  61.     count = obj.count;
  62.     obj.count == nullptr;
  63.     ptr = obj.ptr;
  64.     obj.ptr == nullptr;
  65. }
  66.  
  67. template<class T>
  68. my_shared_ptr<T>::~my_shared_ptr()
  69. {
  70.     if (ptr != nullptr)
  71.     {
  72.         if (*count == 1)
  73.         {
  74.             delete[] ptr;
  75.             delete count;
  76.         }
  77.         else (*count)--;
  78.     }
  79. }
  80.  
  81. template <class T>
  82. T* my_shared_ptr<T>::get()const
  83. {
  84.     return ptr;
  85. }
  86.  
  87. template <class T>
  88. int my_shared_ptr<T>::use_count()const
  89. {
  90.     return *count;
  91. }
  92.  
  93. template <class T>
  94. bool my_shared_ptr<T>::unique()const
  95. {
  96.     return *count == 1;
  97. }
  98.  
  99. template<class T>
  100. inline T& my_shared_ptr<T>::operator=(const my_shared_ptr& obj)
  101. {
  102.     if (this == &obj) return *this;
  103.  
  104.     count = obj.count;
  105.     (*count)++;
  106.     ptr = obj.ptr;
  107.  
  108.     return *this;
  109. }
  110.  
  111. template <class T>
  112. T& my_shared_ptr<T>::operator*()const
  113. {
  114.     return *ptr;
  115. }
  116.  
  117. template <class T>
  118. T* my_shared_ptr<T>::operator->()const
  119. {
  120.     return *ptr;
  121. }
  122.  
  123. template <class T>
  124. T& my_shared_ptr<T>::operator[](int index)const
  125. {
  126.     return ptr[index];
  127. }
  128.  
  129. template <class T>
  130. bool my_shared_ptr<T>::operator==(nullptr_t)const
  131. {
  132.     return ptr == nullptr;
  133. }
  134.  
  135. template <class T>
  136. bool my_shared_ptr<T>::operator==(const my_shared_ptr& obj)const
  137. {
  138.     return ptr == obj.ptr;
  139. }
  140.  
  141. template <class T>
  142. bool my_shared_ptr<T>::operator!=(const my_shared_ptr& obj)const
  143. {
  144.     return ptr != obj.ptr;
  145. }
  146.  
  147. template <class T>
  148. bool my_shared_ptr<T>::operator<(const my_shared_ptr& obj)const
  149. {
  150.     return ptr < obj.ptr;
  151. }
  152.  
  153. template <class T>
  154. bool my_shared_ptr<T>::operator<=(const my_shared_ptr& obj)const
  155. {
  156.     return ptr <= obj.ptr;
  157. }
  158.  
  159. template <class T>
  160. bool my_shared_ptr<T>::operator>(const my_shared_ptr& obj)const
  161. {
  162.     return ptr > obj.ptr;
  163. }
  164.  
  165. template <class T>
  166. bool my_shared_ptr<T>::operator>=(const my_shared_ptr& obj)const
  167. {
  168.     return ptr >= obj.ptr;
  169. }
  170.  
  171. template <class T>
  172. ostream& operator<<<>(ostream& os, my_shared_ptr<T>& obj)
  173. {
  174.     os << *(obj.ptr);
  175.     return os;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement