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/Indexable.hpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 week13/Exercise2/Indexable.hpp (limited to 'week13/Exercise2/Indexable.hpp') diff --git a/week13/Exercise2/Indexable.hpp b/week13/Exercise2/Indexable.hpp new file mode 100644 index 0000000..dd5fb6b --- /dev/null +++ b/week13/Exercise2/Indexable.hpp @@ -0,0 +1,25 @@ +#pragma once +#include "Container.hpp" + +template +class Indexable : virtual public Container { +public: + T& operator[](unsigned index); + const T& operator[](unsigned index) const; +}; + +template +T& Indexable::operator[](unsigned index) { + if (index > this->size) { + throw "Index out of range!"; + } + return this->arr[index]; +} + +template +const T& Indexable::operator[](unsigned index) const { + if (index > this->size) { + throw "Index out of range!"; + } + return this->arr[index]; +} -- cgit v1.2.3