data containers

torhu no at spam.invalid
Fri Jul 4 14:43:48 PDT 2008


Moritz wrote:
> And whats the point of doing:
> ScreenElement[][] layer_map = new ScreenElement[][5];
> 
> Do I HAVE to initialise a number of empty arrays before concatenating 
> instances of classes into it or not?
> I dont see the point in giving some lenght in an dynamic array, for this 
> would make it static, or am I wrong?

You need to add the layers before you can add ScreenElements to them. 
Do you have a fixed number of layers? Then what you suggested will work:

ScreenElement[][] layer_map = new ScreenElement[][5];

If you are used to working with pointers, dynamic arrays are just that, 
except that they store the length of what the pointer points to too. 
It's only that some operations on them actually allocate memory.  "~", 
"~=", and explicitly setting .length allocates.  Indexing does not allocate.


> 
> My whole add_element() function looks like this, just to give you all 
> the info:
> 
> int add_element(ScreenElement new_element, uint layer)
> {
> 	writefln("layer: %d", layer);
> 	writefln("layer_map.length: %d", layer_map.length);
> 		
> 	if(layer <= layer_map.length)
> 	{

Replace "<=" with "<", since layer_map.length always is one past the end 
of the array.


More information about the Digitalmars-d-learn mailing list