Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstddef>
- template <typename T>
- class Array {
- public:
- // конструктор класса, который создает
- // Array размера size, заполненный значениями
- // value типа T. Считайте что у типа T есть
- // конструктор, который можно вызвать без
- // без параметров, либо он ему не нужен.
- //
- explicit Array(size_t size = 0, const T& value = T()) {
- this->table = new T[size];
- this->count = size;
- for (size_t i = 0; i < size; ++i) {
- table[i] = value;
- }
- }
- // Конструктор копирования, который создает
- // опию параметра. Считайте, что для типа
- // T определен оператор присваивания.
- //
- Array(const Array &array) {
- this->table = new T[array.size()];
- this->count = array.size();
- for (size_t i = 0; i < count; ++i) {
- table[i] = array[i];
- }
- }
- // Деструктор, если он вам необходим.
- //
- ~Array() {
- delete [] table;
- }
- // Оператор присваивания.
- //
- Array& operator=(const Array &array) {
- if (this != &array) {
- delete [] table;
- this->table = new T[array.size()];
- this->count = array.size();
- for (size_t i = 0; i < count; ++i) {
- table[i] = array[i];
- }
- }
- return *this;
- }
- // Возвращает размер массива (количество элементов).
- //
- size_t size() const {
- return count;
- }
- // const T& operator[](size_t) const
- // две версии оператора доступа по индексу.
- T& operator[](size_t index) {
- return table[index];
- }
- T operator[](size_t index) const {
- return table[index];
- }
- private:
- T* table;
- size_t count;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement