Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
October 22, 2015 Array of subclasses | ||||
---|---|---|---|---|
| ||||
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(); } |
October 22, 2015 Re: Array of subclasses | ||||
---|---|---|---|---|
| ||||
Posted in reply to DarkRiDDeR | 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
|
October 22, 2015 Re: Array of subclasses | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | >
> 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
|
October 22, 2015 Re: Array of subclasses | ||||
---|---|---|---|---|
| ||||
Posted in reply to DarkRiDDeR | 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
> {
> 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
First of all, the code does not compile with override. It is impossible to override a data. Override should be removed.
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.
|
October 22, 2015 Re: Array of subclasses | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | On Thursday, 22 October 2015 at 12:24:05 UTC, Maxim Fomin wrote:
> 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
>> {
>> 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
>
> First of all, the code does not compile with override. It is impossible to override a data. Override should be removed.
> 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?
|
October 22, 2015 Re: Array of subclasses | ||||
---|---|---|---|---|
| ||||
Posted in reply to DarkRiDDeR | 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 B()];
casting just the first element tells it you want them all to be interpreted as the base class.
I believe that is fixed in the newest version.
|
October 22, 2015 Re: Array of subclasses | ||||
---|---|---|---|---|
| ||||
Posted in reply to DarkRiDDeR | 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 |
October 22, 2015 Re: Array of subclasses | ||||
---|---|---|---|---|
| ||||
Posted in reply to DarkRiDDeR | I found the following solution: abstract class Addon { public string name = "0"; public void updateOfClassFields() { } } class Users: Addon { override { public void updateOfClassFields() { name = "USERS"; } } } activated = [new Users, new Users]; activated[1].updateOfClassFields(); writeln(Core.activated[0].name ~ "\n" ~ Core.activated[1].name); out: 0 USERS |
Copyright © 1999-2021 by the D Language Foundation