Array of subclasses
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Oct 21 23:49:44 PDT 2015
On 10/21/2015 11:14 PM, DarkRiDDeR wrote:
> Hello. I have a class:
>
> abstract class Addon
> {
> public activate(){...}
> ...
> }
>
> its children:
>
> class A: Addon {... }
> class B: Addon {... }
>
> How do I create an array of subclasses Addon? For example, one could to
> do so:
>
> T[2] addons = [new A(), new B()];
> foreach(T addon; addons){
> addon.activate();
> }
>
>
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
More information about the Digitalmars-d-learn
mailing list