D - Copying a class object
- Derek Parnell (32/32) Apr 02 2004 I understand that a class variable holds a reference to the class object...
- Vathix (21/64) Apr 02 2004 I like dup property. Could have:
- Ben Hinkle (15/47) Apr 02 2004 and
I understand that a class variable holds a reference to the class object and is not the class object itself. Which means that if I want to take a copy of the class object, the class needs to have a method defined in it to implement a copy operation, as a simple assignment just copies the reference and not the object. So, is there a standard way of coding the call to that copy method? Maybe an example would help. class Foo { int a; char[] c; this() { ... } Foo opCopy() { Foo x = new Foo; x.a = a; x.c = c; return x; } } and then to invoke a copy of Foo object ... Foo x,y; x = new Foo; y = x.opCopy(); I was just wondering if there is a special syntax or protocol available to invoke a copy operation, or does everybody just do it their own way? If not, something like .... x = new Foo; y := x; would be alright in my view. It would invoke an opCopy() method. -- Derek
Apr 02 2004
Derek Parnell wrote:I understand that a class variable holds a reference to the class object and is not the class object itself. Which means that if I want to take a copy of the class object, the class needs to have a method defined in it to implement a copy operation, as a simple assignment just copies the reference and not the object. So, is there a standard way of coding the call to that copy method? Maybe an example would help. class Foo { int a; char[] c; this() { ... } Foo opCopy() { Foo x = new Foo; x.a = a; x.c = c; return x; } } and then to invoke a copy of Foo object ... Foo x,y; x = new Foo; y = x.opCopy(); I was just wondering if there is a special syntax or protocol available to invoke a copy operation, or does everybody just do it their own way? If not, something like .... x = new Foo; y := x; would be alright in my view. It would invoke an opCopy() method.I like dup property. Could have: interface Dup { Object dup(); } And classes implement that. I suggest this instead of just adding it to Object because all objects aren't dup-able out of the box. class Foo: Dup { private byte[] bar; // Something that needs to be duplicated. this(byte[] barinit) { bar = barinit.dup; } // Arrays have dup. Foo dup() { return new Foo(bar); } // Foo too! (covariant return type) } Foo f = new Foo(something); Foo f2 = f.dup; I tested it and DMD doesn't like covariant returns with interfaces. There's a lot of problems with interfaces. I changed Dup to an abstract class and it worked. -- Christopher E. Miller
Apr 02 2004
"Derek Parnell" <not available.com> wrote in message news:c4k1ua$1fq1$1 digitaldaemon.com...I understand that a class variable holds a reference to the class objectandis not the class object itself. Which means that if I want to take a copyofthe class object, the class needs to have a method defined in it to implement a copy operation, as a simple assignment just copies thereferenceand not the object. So, is there a standard way of coding the call to that copy method?I'm sure other people have different answers but I have two standards :-) either use .dup (see the Array doc) or a constructor. For example for the right definition of Foo Foo x,y; x = new Foo; y = x.dup; y = new Foo(x);Maybe an example would help. class Foo { int a; char[] c; this() { ... } Foo opCopy() { Foo x = new Foo; x.a = a; x.c = c; return x; } } and then to invoke a copy of Foo object ... Foo x,y; x = new Foo; y = x.opCopy();That would work, though I recomment reserving the "op" prefix for actual operators like opAdd, etc. If your suggested operator := is added to the language then opCopy would be fine.I was just wondering if there is a special syntax or protocol available to invoke a copy operation, or does everybody just do it their own way? If not, something like .... x = new Foo; y := x; would be alright in my view. It would invoke an opCopy() method. -- Derek
Apr 02 2004