Templated Interfaces?

Jarrett Billingsley kb3ctd2 at yahoo.com
Sun Aug 24 15:10:56 PDT 2008


"Benji Smith" <dlanguage at benjismith.net> wrote in message 
news:g8sih3$7h4$1 at digitalmars.com...
> Simen Kjaeraas wrote:
>> You do not need to initialize the the array with the "new I!(char)[]". 
>> Just use
>>
>>     I!(char)[] array;
>>     array ~= new C!(char)('a');
>>     ...
>>     char value = array[0].x();
>
> Gotcha.
>
> Coming from Java, it's hard getting used to which types require a 'new' 
> and which ones don't. I find the array types especially confusing.
>
> Thanks for your help!
>
> --benji

Well arrays work _almost_ the same way as in Java, the only difference being 
that they can change their size.

You can either do

auto array = new I!(char)[3];
array[0] = ..
array[1] = ..
array[2] = ..

Or just declare it and append stuff.  The former will be faster since you'll 
only do 1 memory allocation instead of n (although the implementation does 
over-allocate for arrays so that appending is faster, you may not want to 
depend on that for performance-critical code). 




More information about the Digitalmars-d-learn mailing list