「🔨」 fix(ex02): added a const operator[] overload (readonly ofc)
This commit is contained in:
@@ -33,6 +33,15 @@ public:
|
|||||||
return _arr[pos];
|
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 {
|
class outOfBoundException : public std::exception {
|
||||||
virtual const char *what() const throw()
|
virtual const char *what() const throw()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ int main(int, char**)
|
|||||||
//SCOPE
|
//SCOPE
|
||||||
{
|
{
|
||||||
Array<int> tmp = numbers;
|
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;
|
std::cout << "checking if values are the same after a copy..." << std::endl;
|
||||||
|
|||||||
Reference in New Issue
Block a user