File tree 3 files changed +17
-2
lines changed
3 files changed +17
-2
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ class Array
17
17
~Array ();
18
18
19
19
T &operator [](int index);
20
+ const T &operator [](int index) const ;
20
21
int size ();
21
22
};
22
23
Original file line number Diff line number Diff line change @@ -28,6 +28,15 @@ Array<T>::~Array()
28
28
{
29
29
delete [] this ->arr ;
30
30
}
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
+
31
40
template <typename T>
32
41
T &Array<T>::operator [](int index)
33
42
{
Original file line number Diff line number Diff line change @@ -74,11 +74,16 @@ int main(int, char**)
74
74
75
75
// Reading/writing test
76
76
77
-
78
77
std::cout << " numbers[0]: " << numbers[0 ] << std::endl;
79
78
numbers[0 ] = 42 ;
80
79
std::cout << " numbers[0]: " << numbers[0 ] << std::endl;
81
80
82
81
delete [] mirror;//
82
+
83
+ // const test
84
+
85
+ const Array<int > const_check (numbers);
86
+ std::cout << const_check[1 ] << std::endl;
87
+
83
88
return 0 ;
84
- }
89
+ }
You can’t perform that action at this time.
0 commit comments