Is it possible to store different subclasses in one array?

evilrat evilrat666 at gmail.com
Mon Apr 13 05:54:52 UTC 2020


On Monday, 13 April 2020 at 04:21:48 UTC, Leonardo wrote:
>>
>>  foreach (ref gi; GameItems)
>> {
>>     if (gi == Weapon)
>>         gi.Attack()
>> }
>>
>> How would it be?
>
> Replying myself...
>
> weapon = cast(Weapon) gi;
> if (weapon !is null)
>     weapon.Attack()

can be simplified as:

     if (auto weapon = cast(Weapon) gi)
         weapon.Attack();


More information about the Digitalmars-d-learn mailing list