473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Init syntax for checked array template?

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
3 1958
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
5
by: NanQuan | last post by:
I'm hoping someone can help me solve this error since I am at a total loss here. Usually I don't bother posting on any forums or groups on the internet and prefer to solve stuff myself but this is...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
3
by: S. Nurbe | last post by:
Hi, small question concerning pointer: How do I allocate and have access to this pointer in C: float **x; such that I can work with it like an array, e.g. x?
1
by: solomon_13000 | last post by:
connection.asp: <% Sub RunQueryString (pSQL,parms) on error resume next Set conn = Server.CreateObject("ADODB.Connection") conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &...
7
by: recover | last post by:
class Obj { public: Obj(int a){} } class MyContainer { private: Obj m_obj;
1
by: =?UTF-8?B?SmVucyBNw7xsbGVy?= | last post by:
(I also posted this to boost-user) The BGL implementation of breadth-first search uses a dedicated color map. I had the following idea: Some algorithms don't need to distinguish black/gray,...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
12
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.