Implementing .dup / clone for class hierarchies

Bill Baxter dnewsgroup at billbaxter.com
Sun Dec 16 15:59:17 PST 2007


guslay wrote:
> Bill Baxter Wrote:
> 
>> ..
>>
>> The first thread is more about automatic shallow copying and Burton 
>> Radons came up with this compiler-specific hack:
>> """
>>       private extern (C) Object _d_newclass (ClassInfo info);
>>
>>       Object shallow_copy (Object value)
>>       {
>>           if (value is null)
>>               return null;
>>
>>           void *copy = _d_newclass (value.classinfo);
>>           size_t size = value.classinfo.init.length;
>>
>>           copy [8 .. size] = (cast (void *) value) [8 .. size];
>>           return cast (Object) copy;
>>       }
>>
>> Better to start at that offset to avoid copying the synchronisation
>> handle over, plus it's pointless work.
>> """
>>
>> That makes me shudder.  But it wouldn't be fine if it were implemented 
>> as part of object.d.  

Oops.  I meant "it *would* be fine".

I think that functionality would actually be very
>> useful to have as member of ClassInfo.  Then a base class .dup function 
>> could shallow copy everything first so that derived .dups that only add 
>> value members wouldn't need to do anything.
>>
>> --bb
> 
> 
> I think .NET as something similar with MemberwiseClone() [protected]
> 
> http://www.go-mono.com/docs/index.aspx?link=M%3ASystem.Object.MemberwiseClone()
> 

--bb


More information about the Digitalmars-d-learn mailing list