#pragma once template class DynamicArray { void free(); void copyFrom(const DynamicArray& other); protected: T* elems; unsigned allocated; unsigned size; public: DynamicArray(); virtual ~DynamicArray(); DynamicArray(const DynamicArray& other); DynamicArray& operator=(const DynamicArray& other); DynamicArray(DynamicArray&& other); DynamicArray& operator=(DynamicArray&& other); virtual T& operator[](int index) = 0; virtual const T& operator[](int index) const = 0; // Отговорът е не, не можем да използваме оператор += в полморфна йерархия // Понеже String и Numbers не са шаблонни, а използват конкретна "инстанция" на шаблонния клас // тогава полиморфно += би означавало += между разногласни типове, което няма как да стане (добре). virtual DynamicArray& operator+=(const DynamicArray& other) = 0; }; template void DynamicArray::free() { delete[] elems; } template void DynamicArray::copyFrom(const DynamicArray& other) { this->size = other.size; this->allocated = other.allocated; elems = new T[allocated]; for (int i = 0; i < size; i++) { elems[i] = other.elems[i]; } } template DynamicArray::DynamicArray() { elems = nullptr; size = allocated = 0; } template DynamicArray::~DynamicArray() { free(); } template DynamicArray::DynamicArray(const DynamicArray& other) { copyFrom(other); } template DynamicArray& DynamicArray::operator=(const DynamicArray& other) { if (this != &other) { free(); copyFrom(other); } return *this; } template DynamicArray::DynamicArray(DynamicArray&& other) { this->elems = other.elems; other.elems = nullptr; this->second = other.second; other.second = nullptr; this->allocated = other.allocated; this->size = other.size; } template DynamicArray& DynamicArray::operator=(DynamicArray&& other) { if (this != &other) { free(); this->elems = other.elems; other.elems = nullptr; this->allocated = other.allocated; this->size = other.size; } return *this; }