Hi, I have this code that tries to protect the user from array boundary errors.
I don't get why this will compile, tho i've declared the array to be const, therefore, i'm suppose to get a compilation error!
thanks a lot.
/************ file: SafeAccessArray.h ********************/
template<typename T>
class SafeAccessArray
{
private:
int _len;
T * _arr;
public:
SafeAccessArray (int len=2) : _len (len), _arr (new T [len]) {}
~SafeAccessArray () { delete _arr; }
T& operator [](int i) const
{if (i < 0 || i >= _len) throw (-1);
else return _arr[i]; }
};
/************ end of file: SafeAccessArray.h *************/
/************ file: SafeAccessArray1.cpp *************/
#include "SafeAccessArray.h"
int main()`enter code here`
{
SafeAccessArray<int> intArr (2);
intArr[0] = 0;
intArr[1] = 1;
const SafeAccessArray<int> intArrConst (2); // THIS IS THE "PROBLEMATIC" LINE
intArrConst [0] = 0;
intArrConst [1] = 1;
return 0;
}
/************ end of file: SafeAccessArray1.cpp ******/