Bizarre way to 'new' arrays
BCS
BCS at pathlink.com
Fri Jun 16 09:18:41 PDT 2006
Tom S wrote:
> Sean Kelly wrote:
>
>> Sadly, this isn't legal:
>>
>> int* i = new int(5);
>>
>> To allocate and initialize an integer. AFAIK there's no way around
>> having the assignment as a separate statement following the allocation.
>
>
> int* i = (new int)[0..1] = 5;
>
>
import std.stdio;
void main()
{ // works for more than one value
const static int[] store = [0,1,2,3,4,5,6,7,8,9];
// get a value
int* i = store[5..6].dup.ptr;
writef(*i,\n);
// change it
*i = 4;
writef(*i,\n);
// original is unchanged
i = store[5..6].dup.ptr;
writef(*i,\n);
}
More information about the Digitalmars-d
mailing list