Newbie: copy, assignment of class instances

Larry Luther larry.luther at dolby.com
Thu May 20 16:12:22 PDT 2010


I did not get an error when building and running with DMD 2.042:

import std.stdio;


class A {
  int x, y;

  void copy (in A a) {
    x = a.x;
    y = a.y;
  }

  void dump (string s) {
    writefln( "%s.A = { %s, %s }", s, x, y);
  }
}

class B : A {
  int z;

  void copy (in B b) {
    super.copy( b);
    z = b.z;
  }

  void dump (string s) {
    super.dump( s);
    writefln( "%s.B = { %s }", s, z);
  }
}


void main () {
  B foo = new B;
  B bar = new B;

  foo.x = 3;
  foo.y = 5;
  foo.z = 17;

  bar.x = 7;
  bar.y = 11;
  bar.z = 13;

  foo.dump( "foo");
  bar.dump( "bar");

  bar.copy( foo);
  bar.dump( "bar#2");
}

Which prints:

foo.A = { 3, 5 }
foo.B = { 17 }
bar.A = { 7, 11 }
bar.B = { 13 }
bar#2.A = { 3, 5 }
bar#2.B = { 17 }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20100520/3c815691/attachment.html>


More information about the Digitalmars-d-learn mailing list