Shallow copy object when type is know

rumbu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 21 04:53:13 PDT 2016


On Wednesday, 20 April 2016 at 12:32:48 UTC, Tofu Ninja wrote:
> Is there a way to shallow copy an object when the type is 
> known? I cant seem to figure out if there is a standard way. I 
> can't just implement a copy function for the class, I need a 
> generic solution.

extern (C) Object _d_newclass(TypeInfo_Class ci);

Object dup(Object obj)
{
     if (obj is null)
         return null;
     ClassInfo ci = obj.classinfo;
     size_t start = Object.classinfo.init.length;
     size_t end = ci.init.length;
     Object clone = _d_newclass(ci);
     (cast(void*)clone)[start .. end] = (cast(void*)obj)[start .. 
end];
     return clone;
}



More information about the Digitalmars-d-learn mailing list