Array type conversion

Thomas Kuehne thomas-dloop at kuehne.cn
Mon Apr 30 02:42:49 PDT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mark Burnett schrieb am 2007-04-28:
> Still there are one or two things that strike me as odd:  in particular
> that arrays of a derived type can be converted to an array of a base type.

[...]

Below is a simplified sample:

# import std.stdio;
#
# class Base{
#    int x;
# }
#
# class Derived : Base{
#    long y;
# }
#
# void main(){
#    Derived[] derived = new Derived[1];
#    Base[] base;
#
#    derived[0] = new Derived();
#    derived[0].x = 1;
#    derived[0].y = 2;
#    writefln("derieved[0] -> (x:%s, y:%s)", derived[0].x, derived[0].y);
#
#    base  = derived; // <- this is the issue
#    writefln("base[0] -> (x:%s)", base[0].x);
#
#    base[0] = new Base();
#    base[0].x = 3;
#    writefln("base[0] -> (x:%s)", base[0].x);
#
#    writefln("derieved[0] -> (x:%s, y:%s) !!!random y!!!", derived[0].x, derived[0].y);
# }

Thomas


-----BEGIN PGP SIGNATURE-----

iD8DBQFGNcY1LK5blCcjpWoRArn8AJ9yGL1zyYJZRea2odm0ZPNzebpGnQCeI219
X6rZ2SXWKt1ZF3dGxMol+Ag=
=EClm
-----END PGP SIGNATURE-----



More information about the Digitalmars-d mailing list