Advertisement
RobertDeMilo

RB5.6-7 Конструктор копирования и оператор присваивания. Конструктор перемещения и перемещающий оп-р

Apr 17th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. //#include <iostream>
  2. //#include <vector>
  3. //
  4. //using namespace std;
  5. //
  6. //class Logger
  7. //{
  8. //public:
  9. //  Logger()
  10. //  {
  11. //      cout << "Default ctor\n";
  12. //  }
  13. //  Logger(const Logger&)
  14. //  {
  15. //      cout << "Copy ctor\n";
  16. //  }
  17. //  void operator=(const Logger&)
  18. //  {
  19. //      cout << "Copy assignment\n";
  20. //  }
  21. //};
  22. //
  23. //int main()
  24. //{
  25. //  Logger source;
  26. //  Logger target = source;
  27. //
  28. //  vector<Logger>  loggers;
  29. //  loggers.push_back(target);
  30. //
  31. //  source = target;
  32. //}
  33.  
  34. #include <iostream>
  35. #include <algorithm>
  36. #include <vector>
  37.  
  38. using namespace std;
  39.  
  40. template <typename T>
  41. class SimpleVector
  42. {
  43. public:
  44.  
  45.     SimpleVector() = default;
  46.  
  47.     explicit SimpleVector(size_t size);
  48.     SimpleVector(const SimpleVector<T>& other);
  49.     //rvalue reference ведет себя так же как ссылка, но позволяет принимать временный объект
  50.     SimpleVector(SimpleVector&& other);
  51.  
  52.     void operator=(const SimpleVector& other);
  53.     void operator=(SimpleVector&& other);
  54.  
  55.     ~SimpleVector();
  56.  
  57.     T& operator[](size_t index);
  58.  
  59.     T* begin();
  60.     T* end();
  61.    
  62.     const T* begin() const;
  63.     const T* end() const;
  64.  
  65.     size_t Size() const;
  66.     size_t Capacity() const;
  67.     void PushBack(const T& value);
  68.  
  69. private:
  70.  
  71.     T* data = nullptr;
  72.     size_t size = 0;
  73.     size_t capacity = 0;
  74. };
  75.  
  76. template <typename T>
  77. SimpleVector<T>::SimpleVector(size_t size) : data(new T[size]), size(size), capacity(size)
  78. {
  79.  
  80. }
  81.  
  82. template <typename T>
  83. SimpleVector<T>::SimpleVector(const SimpleVector<T>& other)
  84.     :data(new T[other.capacity]), size(other.size), capacity(other.capacity)
  85. {
  86.     copy(other.begin(), other.end(), begin());
  87. }
  88.  
  89. template<typename T>
  90. SimpleVector<T>::SimpleVector(SimpleVector&& other)
  91.     :data(other.data), size(other.size), capacity(other.capacity)
  92. {
  93.     other.data = nullptr;
  94.     other.size = other.capacity = 0;
  95. }
  96.  
  97. template<typename T>
  98. void SimpleVector<T>::operator=(const SimpleVector& other)
  99. {
  100.     delete[]data;
  101.     data = new T[other.capacity];
  102.     size = other.size;
  103.     capacity = other.capacity;
  104.     copy(other.begin(), other.end(), begin());
  105. }
  106.  
  107. template<typename T>
  108. void SimpleVector<T>::operator=(SimpleVector&& other)
  109. {
  110.     delete[]data;
  111.     data = other.data;
  112.     size = other.size;
  113.     capacity = other.capacity;
  114.     copy(other.begin(), other.end(), begin());
  115.  
  116.     other.data = nullptr;
  117.     other.size = other.capacity = 0;
  118. }
  119.  
  120. template <typename T>
  121. SimpleVector<T>::~SimpleVector()
  122. {
  123.     delete[]data;
  124. }
  125.  
  126. template <typename T>
  127. T& SimpleVector<T>::operator[](size_t index)
  128. {
  129.     return data[index];
  130. }
  131.  
  132. template <typename T>
  133. size_t SimpleVector<T>::Size() const
  134. {
  135.     return size;
  136. }
  137.  
  138. template <typename T>
  139. size_t SimpleVector<T>::Capacity() const
  140. {
  141.     return capacity;
  142. }
  143.  
  144. template <typename T>
  145. void SimpleVector<T>::PushBack(const T& value)
  146. {
  147.     if (size >= capacity)
  148.     {
  149.         auto new_cap = capacity == 0 ? 1 : 2 * capacity;
  150.         auto new_data = new T[new_cap];
  151.         copy(begin(), end(), new_data);
  152.         delete[]data;
  153.  
  154.         data = new_data;
  155.         capacity = new_cap;
  156.     }
  157.     data[size++] = value;
  158. }
  159.  
  160. template <typename T>
  161. T* SimpleVector<T>::begin()
  162. {
  163.     return data;
  164. }
  165.  
  166. template <typename T>
  167. const T* SimpleVector<T>::begin() const
  168. {
  169.     return data;
  170. }
  171.  
  172. template <typename T>
  173. T* SimpleVector<T>::end()
  174. {
  175.     return data + size;
  176. }
  177.  
  178. template <typename T>
  179. const T* SimpleVector<T>::end() const
  180. {
  181.     return data + size;
  182. }
  183.  
  184. int main()
  185. {
  186.    /* SimpleVector<int> source(1);
  187.     source[0] = 8;
  188.     SimpleVector<int> target= source;
  189.  
  190.     cout << source[0] << " " << target[0] << endl;*/
  191.  
  192.     /*SimpleVector<int> source(1);
  193.     SimpleVector<int> target = move(source);
  194.  
  195.     cout << source.Size() << " " << target.Size() << endl;*/
  196.  
  197.     SimpleVector<int> source(1);
  198.     SimpleVector<int> target(1);
  199.     target = move(source);
  200.  
  201.     cout << source.Size() << " " << target.Size() << endl;
  202.  
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement