Hi. I recently wrote a simple little template that defines an array
that checks attempts to use out of bounds indexes. The only problem is
that it does provide the use array style value initialization when the
type is instantiated. Any suggestions for a mod that would allow array
initialization syntax?
***********Here's the type:
#ifndef CHECKED_ARRAY_
#define CHECKED_ARRAY_
template <class ITEM_TYPE, int MAX_SIZE>
class CheckedArray
{
public:
ITEM_TYPE& operator[](int itemIndex)
{
assert( (itemIndex >= 0) && (itemIndex < MAX_SIZE) );
return items[itemIndex];
}
private:
ITEM_TYPE items[MAX_SIZE];
};
#endif /*CHECKED_ARRAY_*/
***********And here's how to use it:
#include "CheckedArray.h"
CheckedArray<int, 10> intArray; //creates an int array of size 10
CheckedArray<SomeClass*, 100> someClassArray; //creates an array of
pointers to SomeClass
intArray[0] = 1; //works just like a regular array
intArray[10] = 3; //assert occurs, since an index of 10 is out of range
**************
Thanks for any suggestions!
Ken