🔨」 fix(ex02): added a const operator[] overload (readonly ofc)

This commit is contained in:
2025-08-18 20:01:42 +02:00
parent e0535f2b4a
commit cbd45d0f14
2 changed files with 11 additions and 1 deletions

View File

@@ -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()
{

View File

@@ -17,7 +17,8 @@ int main(int, char**)
//SCOPE
{
Array<int> tmp = numbers;
Array<int> test(tmp);
const Array<int> 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;