Initialising multidimensional dynamic arrays

Mike James via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Oct 1 01:23:16 PDT 2014


On Wednesday, 1 October 2014 at 08:08:06 UTC, ketmar via 
Digitalmars-d-learn wrote:
> On Wed, 01 Oct 2014 07:45:48 +0000
> Mike James via Digitalmars-d-learn 
> <digitalmars-d-learn at puremagic.com>
> wrote:
>
>> so in the constructor...
>> 
>> this(size_t x, size_t y) {
>>    mda = new MyDataArray[](x);
>>          foreach(n, _; mda) mda[n].data.length = y;
>> }
>> 
>> Is there a simpler way?
> sorry, but no. btw, if MyDataArray is struct, you should do 
> this:
>
>   foreach (ref m; mda) m.data.length = y;
>
> or even this:
>
>   foreach (ref m; mda = new MyDataArray[](x)) m.data.length = x;
>
>
> the thing is that without 'ref' you operates on the local copy, 
> not on
> the real array element.

Thanks ketmar, that did the trick.

Regards, -=mike=-


More information about the Digitalmars-d-learn mailing list