Shallow copy object when type is know

Tofu Ninja via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 20 12:58:15 PDT 2016


On Wednesday, 20 April 2016 at 18:48:58 UTC, Alex Parrill wrote:
> 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.
>
> A generic class copy function would require accessing private 
> fields, so a clean per-attribute class copy is impossible. 
> Doing a bitwise copy might work except for the synchronization 
> mutex pointer.
>
> Can you elaborate on why you need this?

To implement a copy/paste/duplicate functionality in a game 
editor. I have an entity-component system, to duplicate an 
entity, all it's components need to be duplicated. I have many 
many components, I don't want to rely on manually writing copy 
methods for each, it is too error prone. There are only a very 
few that need special copies, almost all can get by with a simple 
shallow copy. I would like a generic way to do that shallow copy.

How does D not have shallow copy? Seems like a very basic 
functionality...


More information about the Digitalmars-d-learn mailing list