Array initializers and inheritance

Max Bolingbroke "batterseapower{no" at sp4/\\/\\}hotmail.com
Sun Oct 29 04:01:47 PST 2006


Hi,

I have an array declared like so:

private GameObject[] _gameObjects;

And an inheritance hierarchy like this:

class Player : GameObject
{ ... }

class Target : GameObject
{ ... }

When I do this:

_gameObjects = [new Player(),
                 new Target(Direction.Up, 0, 0)];

I get a compile error (Target cannot be cast to Player). But when I do this:

_gameObjects = [cast(GameObject)(new Player()),
                 new Target(Direction.Up, 0, 0)];

It works! Is this by design? It is not clear to me from reading the 
specification.

Thanks,

Max



More information about the Digitalmars-d-learn mailing list