Array of subclasses

2015-10-22 Thread DarkRiDDeR via Digitalmars-d-learn
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

Re: Array of subclasses

2015-10-22 Thread DarkRiDDeR via Digitalmars-d-learn
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

Re: Array of subclasses

2015-10-22 Thread Adam D. Ruppe via Digitalmars-d-learn
On Thursday, 22 October 2015 at 06:14:34 UTC, DarkRiDDeR wrote: T[2] addons = [new A(), new B()]; Until pretty recently the compiler was a little picky about the types here so you might have to explicitly cast the first element to the base clas type. T[2] addons = [cast(T) new A(), new

Re: Array of subclasses

2015-10-22 Thread Maxim Fomin via Digitalmars-d-learn
On Thursday, 22 October 2015 at 13:29:06 UTC, DarkRiDDeR wrote: I don't need the base class data. How to create a array of subclasses objects with the derived data members? The language is implemented in this way. You have already have the answer: writeln(Core.users.name) Out: USERS

Re: Array of subclasses

2015-10-22 Thread DarkRiDDeR via Digitalmars-d-learn
I found the following solution: abstract class Addon { public string name = "0"; public void updateOfClassFields() { } } class Users: Addon { override { public void updateOfClassFields() {

Re: Array of subclasses

2015-10-22 Thread DarkRiDDeR via Digitalmars-d-learn
. The reason it works this way is that the first access is to base class data while the second is to the derived data member. I don't need the base class data. How to create a array of subclasses objects with the derived data members?

Re: Array of subclasses

2015-10-22 Thread Maxim Fomin via Digitalmars-d-learn
On Thursday, 22 October 2015 at 11:02:05 UTC, DarkRiDDeR wrote: This variant works strangely. Example: abstract class Addon { public string name = "0"; } class Users: Addon { override { public string name = "USERS"; } } static final class Core {

Re: Array of subclasses

2015-10-22 Thread Ali Çehreli via Digitalmars-d-learn
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