Coder's Guild Mailing List

Re: need help with arrays

Posted by Fox on 2000-05-03

> Okay I have a problem here and it's really bugging me that I can't figure it
> out. I need a struct or preferably a class that can be defined as an array.
> As an example, something like this:
> 
> class someclass
> {
>     bool var1;
>     boo var2;
> } somename[21];
> 
> somename[1].var1 = something;
> 
> is it possible to do something like this? cause it's just not practical to
> have to name every variable of type someclass other than to name it as a
> number.

/***********************************/

	Oh yes, it is possible to have an array of classes/structs.  You access them 
just like you showed above, and that is one way of defining a global array of 
classes.  Just remember, that the access specifiers(public, protected, and 
private) still apply.
	Also remember that when you say somename[21], "21" is the number of 
classes on your array(count starts at zero and goes to 20!), so 21 is not the last 
element on your array, and you should NEVER do something like this:

for(i = 0; i <= 21; i++)         //this will eventually lead to problems!!!!  Never do!
//accss the array with i.

instead:

for(i = 0; i < 21; i++)        //this is the right way.
//access array with i


	21 is not the last element on your array, but the number of classes on the 
array!  The last element is the number of classes on your array minus one.  
Remember that!  Otherwise, you'll overwrite memory outside the array bounds.
	Just a few tips that I've violated myself.
*******************************************
My brain is powered by C++
My hands are trained for the keyboard
My legs are built for the hockey floor
My ears are tuned to Mike Oldfield
My eyes are sensitive to video games
My attitude is sarcastic
My body is the work of God!