Array of subclasses

DarkRiDDeR via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 22 04:02:01 PDT 2015


>
> This works:
>
> abstract class Addon {
>     public void activate() {
>     }
> }
>
> class A: Addon {}
> class B: Addon {}
>
> void main() {
>     Addon[2] addons = [new A(), new B()];
> }
>
> This works too:
>
>     Addon[] addons = [new A(), new B()];
>
> I am happy to report that even the following works with dmd 
> 2.069.0-b2:
>
>     auto addons = [new A(), new B()];
>
> I think the last one used to not work. Apparently now their 
> "common type" is inferred correctly.
>
> Ali

This variant works strangely. Example:

abstract class Addon
{
	public string name = "0";
}
class Users: Addon
{
	override
	{
		public string name = "USERS";
	}
}
static final class Core
{
	static:
		public Addon[] activated;
		public Users users;
		
		public void activate()
		{
			users = new Users;
			activated = [new Users, new Users];
		}
}

Core.activate();
writeln(Core.users.name ~ "\n"  ~ Core.activated[1].name);

Out:
USERS
0




More information about the Digitalmars-d-learn mailing list