From 9c11fababb9b6b194e646fbeb62d141aacf74c43 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 25 May 2024 16:08:17 +0300 Subject: [w13] Added solutions --- week13/Exercise2/Container.hpp | 75 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 week13/Exercise2/Container.hpp (limited to 'week13/Exercise2/Container.hpp') diff --git a/week13/Exercise2/Container.hpp b/week13/Exercise2/Container.hpp new file mode 100644 index 0000000..ace942f --- /dev/null +++ b/week13/Exercise2/Container.hpp @@ -0,0 +1,75 @@ +#pragma once + +template +class Container { + void free(); + void copyFrom(const Container& other); + +protected: + T* arr; + unsigned size; + +public: + Container(); + ~Container(); + Container(const Container& other); + Container& operator=(const Container& other); + Container(Container&& other); + Container& operator=(Container&& other); +}; + +template +void Container::free() { + delete[] arr; +} + +template +void Container::copyFrom(const Container& other) { + this->size = other.size; + this->arr = new T[size]; + for (int i = 0; i < size; i++) { + this->arr[i] = other.arr[i]; + } +} + +template +Container::Container() { + this->arr = nullptr; + size = 0; +} + +template +Container::~Container() { + free(); +} + +template +Container::Container(const Container& other) { + copyFrom(other); +} + +template +Container& Container::operator=(const Container& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +template +Container::Container(Container&& other) { + this->arr = other.arr; + other.arr = nullptr; +} + +template +Container& Container::operator=(Container&& other) { + if (this != &other) { + free(); + + this->arr = other.arr; + other.arr = nullptr; + } + return *this; +} -- cgit v1.2.3