Array of class intances

Simen Kjaeraas simen.kjaras at gmail.com
Wed Apr 9 15:27:34 PDT 2008


On Wed, 09 Apr 2008 20:52:05 +0200, YY <yyudhistira at hotmail.com> wrote:

>
>> Something like this (untested, it may need debugging!) may be useful  
>> for nested arrays (IsArray is true for dynamic or static arrays and  
>> false in every other situation):
>>
>> template DeconstArrType(T) {
>>     // similar to std.bind.DynamicArrayType
>>     static if (IsArray!(T))
>>         alias typeof(T[0])[] DeconstArrType;
>>     else
>>         alias T DeconstArrType;
>> }
>>
>> DeconstArrType!(TySub)[] deepDup(TySub)(TySub[] seq) {
>>     static if (is(typeof(TySub.dup))) {
>>         auto result = new DeconstArrType!(TySub)[seq.length];
>>         foreach (i, sub; seq)
>>             result[i] = deepDup(sub);
>>         return result;
>>     } else {
>>         return seq.dup;
>>     }
>> }
>>
>> A generic deepcopy() function that works with everything (that needs  
>> the support of a special method in Object class too) may be useful.
>>
>
> Great, thanks for the hints. I have another question. Do I have to  
> delete each and every member of the array? Like this :
>
> foreach (ref i; inst) delete i;
> inst.length = 0;
>
> If I only do this :
>
> inst.length = 0;
>
> will all the instance items be automatically captured by garbage  
> collector?

I believe the easiest would be to do inst = null;


More information about the Digitalmars-d-learn mailing list