Is it possible to store different subclasses in one array?

Leonardo leotada523 at gmail.com
Mon Apr 13 12:17:37 UTC 2020


On Monday, 13 April 2020 at 05:54:52 UTC, evilrat wrote:
> 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();

Even better. Thanks folks!


More information about the Digitalmars-d-learn mailing list