Templated Interfaces?
Ary Borenszweig
ary at esperanto.org.ar
Mon Aug 25 04:30:10 PDT 2008
Benji Smith a écrit :
> Jarrett Billingsley wrote:
>> 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).
>
> Huh. I thought that, by putting an integer literal into the array
> declaration, it'd be declared as a static array rather than a dynamic
> array.
If you put an integer into the array declaration, it *is* declared as a
static array.
Static array:
-------------
auto array = new I!(char)[3];
- array is of type I!(char)[3] (static array)
- you cannot append using ~
- you can assign or access using indices
- you can initialize it like that or using an array literal
Dynamic array:
--------------
I!(char)[] array;
- array is of type I!(char)[] (dynamic array)
- you can append using ~
- you can assign or access using indices
- you can initialize it using an array literal
More information about the Digitalmars-d-learn
mailing list