Now, class T could be anything and specifically I want T to be <int> or
<MyCustomClass *>.

Now in the 5th line, it is checking for out of bounds error. The question is I do not know what to return if there is out of bounds. I have NULL for now, but if T is not a pointer, the program won't like it.

12-15-2005

grumpy

You might also try throwing an exception .....

12-15-2005

Hiro

Yes, that what I decided to do. I'll throw an exception
and for each derived class, I'll wrap around that function and handle any errors.

12-15-2005

Daved

If VectorOfT is a vector, then you don't have to check the bounds. The at() function will check the bounds and throw an exception for you.

If you'd prefer to have your function not throw an exception, then you could return T() which returns a default initialized value. If T is an int, it will return 0, if it is a pointer it will return 0 (a null pointer). If T is a class type, it will return a default constructed object. This would add a requirement to T that it be default constructable (if that isn't already required).

You should also use [] instead of at() if you are going to check for all out of bounds indexes yourself, since you wouldn't need the bounds checking frorm at(). Of course, the current code only checks for one type of bad index, so at() is more comprehensive than that.