Return value of 'getter' function when out of bounds?

Hi

I have a class which contains an array and there are setter and getter functions to change/return the elements of this array.

I have two queries:

1. Is it a good design practice for these setters and getters to be responsible for bounds checking? Or should this be left to calling program?

2. For the getter function (the input being a pos of the array) how would you notify the calling function that pos is not within bounds? You can't return -1 because the array could contain negative numbers. I thin std::string throws an exception but unfortunately I'm writing on a platform where this isn't possible.

Many thanks
1. Both std::vector and std::string have two [gs]etters: the at() and the op[]. One throws, the other might not. The one throwing obviously must check bounds. The other?


2. Q: What is common for these:
http://www.cplusplus.com/reference/ios/ios/operator_bool/
http://www.cplusplus.com/reference/cstdio/ferror/
A: They tell, whether the previous IO-operation was successful. That info has been stored somewhere (class or global).

Plan B: return two values:
http://doc.qt.io/qt-5/qstring.html#toDouble
Topic archived. No new replies allowed.