By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Init syntax for checked array template?

P: n/a
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

Apr 24 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
kk****@yahoo.com wrote:
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


Apr 24 '06 #2

P: n/a
kk****@yahoo.com wrote:
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

Apr 24 '06 #3

P: n/a
mlimber wrote:
<snip>
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

Apr 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.