Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

How to copy an object

Moderators: larsivi kris

Posted: 03/28/08 16:45:12

Does anyone know how to make a copy of an object class, rather than creating a reference?

I would like to create something that gives me the following results:

import tango.text.convert.Integer;

class A
       int x,y;

A[] bodies;

int main()


       bodies[5] = new A;
       bodies[5].x = 5;

       A a = bodies[5];


       Cout(.toString(bodies[5].x)~"\n"); // Prints 5
       Cout(.toString(a.x)~"\n");         // Prints 6

       return 0;

Thanks, Mason

Author Message

Posted: 03/28/08 17:03:35

A a = bodies[5].dup;

Add a .dup() method to your class as required?

Posted: 03/28/08 17:36:35 -- Modified: 03/28/08 20:36:10 by

kris wrote:
A a = bodies[5].dup;

Add a .dup() method to your class as required?

Hi, Kris.

I guess my follow up question would be: how do to I create a .dup() memeber function that returns a copy of the class rather than a reference?

In any case, I think I'm going to create a temporary structure with all the data points that I want to copy. Am I correct in referring to this as a shallow copy?

Thanks, Mason