Skip to content

Commit a19bde1

Browse files
committed
ex02 const fixed
1 parent 20d2a90 commit a19bde1

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

cpp_module_07/ex02/Array.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Array
1717
~Array();
1818

1919
T &operator[](int index);
20+
const T &operator[](int index) const;
2021
int size();
2122
};
2223

cpp_module_07/ex02/Array.tpp

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ Array<T>::~Array()
2828
{
2929
delete [] this->arr;
3030
}
31+
32+
template <typename T>
33+
const T &Array<T>::operator[](int index) const
34+
{
35+
if (index < 0 || index >= this->arrSize)
36+
throw std::exception();
37+
return (this->arr[index]);
38+
}
39+
3140
template <typename T>
3241
T &Array<T>::operator[](int index)
3342
{

cpp_module_07/ex02/main.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,16 @@ int main(int, char**)
7474

7575
// Reading/writing test
7676

77-
7877
std::cout << "numbers[0]: " << numbers[0] << std::endl;
7978
numbers[0] = 42;
8079
std::cout << "numbers[0]: " << numbers[0] << std::endl;
8180

8281
delete [] mirror;//
82+
83+
// const test
84+
85+
const Array<int> const_check(numbers);
86+
std::cout << const_check[1] << std::endl;
87+
8388
return 0;
84-
}
89+
}

0 commit comments

Comments
 (0)