Array of class intances

YY yyudhistira at hotmail.com
Wed Apr 9 11:52:05 PDT 2008


> 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?



More information about the Digitalmars-d-learn mailing list