Init syntax for checked array template?

K

kk_oop

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
 
K

kk_oop

Correction to the original first paragraph. I had a cut and paste
problem....

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 NOT provide the use of array style value initialization
when the type is instantiated. Any suggestions for a mod that would
allow array initialization syntax?

Thanks again,

Ken
 
M

mlimber

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

Compare Boost.Array, which offers checked access through at():

http://boost.org/doc/html/array.html

Particularly see the design rationale section:

http://boost.org/doc/html/array/rationale.html

This library is also part of TR1, likely extensions to the standard
library, and so I'd recommend you use the existing implementation
rather than rolling your own.

Cheers! --M
 
K

kk_oop

mlimber wrote:
Compare Boost.Array, which offers checked access through at():

http://boost.org/doc/html/array.html

Particularly see the design rationale section:

http://boost.org/doc/html/array/rationale.html

This library is also part of TR1, likely extensions to the standard
library, and so I'd recommend you use the existing implementation
rather than rolling your own.

Cheers! --M

It does not seem like the boost class asserts on out of bounds array
indexes. Is this true?

Also, I realize now that I'd prefer to make the MAX_SIZE template
parameter a constructor parameter. Making it a template parameter
requires all function parameters of type CheckedArray to specify the
max size. Making it a constructor parameter mitigates this. So
instead of

template <class ITEM_TYPE, int MAX_SIZE>
....

you end up with something like this:

template <class ITEM_TYPE>
class CheckedArray
{
public:

CheckedArray(unsigned int theSize) : size(theSize)
{
items = new ITEM_TYPE[size];
}

ITEM_TYPE& operator[](int itemIndex)
{
assert( itemIndex < size );
return items[itemIndex];
}

private:

ITEM_TYPE* items;
unsigned int size;

};

#endif /*CHECKED_ARRAY_*/

I stated the reason for doing it this way, however, is there a reason
against having the size be a constructor parameter?

Thanks,

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top