Advertisement
Gistrec

MyArray (у T нет оператора присваивания)

Jan 19th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <cstddef>
  2.  
  3. template <typename T>
  4. class Array {
  5. public:
  6.     //   конструктор класса, который создает
  7.     //   Array размера size, заполненный значениями
  8.     //   value типа T. Считайте что у типа T есть
  9.     //   конструктор, который можно вызвать без
  10.     //   без параметров, либо он ему не нужен.
  11.     //
  12.     explicit Array(size_t size = 0, const T& value = T()) {
  13.         this->table = static_cast<T*>(operator new[] (size * sizeof(T)));
  14.         this->count = size;
  15.         for (size_t i = 0; i < size; ++i) {
  16.             //здесь память для объекта не выделяется, но инициализируется
  17.             new (table + i) T(value);
  18.         }
  19.     }
  20.  
  21.     // Конструктор копирования, который создает
  22.     // опию параметра. Считайте, что для типа
  23.     // T определен оператор присваивания.
  24.     //
  25.     Array(const Array<T> &array) {
  26.         this->table = static_cast<T*>(operator new[] (array.size() * sizeof(T)));
  27.         this->count = array.size();
  28.         for (size_t i = 0; i < count; ++i) {
  29.             new (table + i) T(array[i]);
  30.         }
  31.     }
  32.  
  33.     // Деструктор, если он вам необходим.
  34.     //
  35.     ~Array() {
  36.         for (size_t i = 0; i < count; ++i) {
  37.             table[i].~T();
  38.         }
  39.         operator delete[] (table);
  40.     }
  41.  
  42.     // Оператор присваивания.
  43.     //
  44.     Array& operator=(const Array<T> &array) {
  45.         if (this != &array) {
  46.             operator delete[] (table);
  47.             /*this->table = new T[array.size()];
  48.             this->count = array.size();
  49.             for (size_t i = 0; i < count; ++i) {
  50.                 table[i] = array[i];
  51.             }*/
  52.             this->table = static_cast<T*>(operator new[] (array.size() * sizeof(T)));
  53.             this->count = array.size();
  54.             for (size_t i = 0; i < count; ++i) {
  55.                 //здесь память для объекта не выделяется, но инициализируется
  56.                 new (table + i) T(array[i]);
  57.             }
  58.         }
  59.         return *this;
  60.     }
  61.  
  62.     // Возвращает размер массива (количество элементов).
  63.     //
  64.     size_t size() const {
  65.         return count;
  66.     }
  67.  
  68.     // const T& operator[](size_t) const
  69.     //   две версии оператора доступа по индексу.
  70.     T& operator[](size_t index) {
  71.         return table[index];
  72.     }
  73.  
  74.     T operator[](size_t index) const {
  75.         return table[index];
  76.     }
  77.  
  78. private:
  79.     T* table;
  80.     size_t count;
  81. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement