From cbd45d0f14b8da8f97edd569a07dd1a3a80977e2 Mon Sep 17 00:00:00 2001 From: yosyo Date: Mon, 18 Aug 2025 20:01:42 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix(ex02):=20a?= =?UTF-8?q?dded=20a=20const=20operator[]=20overload=20(readonly=20ofc)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex02/Array.hpp | 9 +++++++++ ex02/main.cpp | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ex02/Array.hpp b/ex02/Array.hpp index 5ef147f..853f237 100644 --- a/ex02/Array.hpp +++ b/ex02/Array.hpp @@ -33,6 +33,15 @@ public: return _arr[pos]; } + T &operator[](const size_t &pos) const + { + if (pos < 0 || pos >= _size) + { + throw outOfBoundException(); + } + return _arr[pos]; + } + class outOfBoundException : public std::exception { virtual const char *what() const throw() { diff --git a/ex02/main.cpp b/ex02/main.cpp index b8f4f87..030284a 100644 --- a/ex02/main.cpp +++ b/ex02/main.cpp @@ -17,7 +17,8 @@ int main(int, char**) //SCOPE { Array tmp = numbers; - Array test(tmp); + const Array test(tmp); + std::cout << "accessing a const value : " << (int) test[15] << std::endl; } std::cout << "checking if values are the same after a copy..." << std::endl;