0

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 ******/
4
  • 3
    SafeAccessArray needs a copy constructor and assignment operator. Look up the Rule of Three, and the copy-and-swap idiom. Even if this were a good idea, just use std::vector privately and be done with it. Also, don't throw integers, throw things deriving from std::exception; std::out_of_range awaits. Commented Oct 2, 2010 at 19:14
  • 2
    Please don't throw -1, not even in test code. Throw ::std::out_of_range or something, anything but a bare base type like int or char *, and preferably something derived from ::std::exception. Commented Oct 2, 2010 at 19:21
  • 2
    In addition, new[] goes with delete[], not delete. Commented Oct 2, 2010 at 19:21
  • In case you're using G++/libstdc++, just use std::vector, compile with -D_GLIBCXX_DEBUG and enjoy libstdc++'s debug mode. Other compiler vendors probably offer a similar feature. Commented Oct 2, 2010 at 19:28

2 Answers 2

4

Yea it's const, but you did T& operator [](int i) const anyway. You're returning a reference, and this function can be called on a const object.

Make it return const T&. Better yet, stop. Just use std::vector and the at() function.

Sign up to request clarification or add additional context in comments.

4 Comments

so, just to make it clear, when i call "intArrConst [0] = 0" - operator[] will cause a cast to a non-const object, or the assignment won't really happen on my const object?
@limlim: What? When you say intArrConst[0], T& operator [](int i) const is called. That results in a T&, and then you assign 0 to it, changing the reference (which, being an alias, just changes the value it's aliasing.)
@limlim: The class contains T * _arr; a const instance of the class contains a T *const _arr whereas you want it to have a T const *const _arr. C++ did not add the extra qualification you wanted, so the guarantee is your responsibility.
@jenny: If an answer solved your problem, please check the checkmark next to it.
2

I think that operator[] member function desires the following two overloaded variants:

T& operator [](int i);
const T& operator [](int i) const;

The one provided

T& operator [](int i) const;

does not match any of the above, and hence the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.