Is this the proper way to do it?

Jack jckj33 at gmail.com
Mon Feb 22 21:46:01 UTC 2021


On Saturday, 13 February 2021 at 07:08:58 UTC, mw wrote:
> On Saturday, 13 February 2021 at 05:52:34 UTC, Jack wrote:
>> I have a base class A, where I make specific operator 
>> depending on the derived class type. Currently I'm using 
>> something like this:
>>
>> c is a class derived from A
>> bool shouldDoX = (cast(X)c) !is null || (cast(Y)c) !is null || 
>> (cast(K)c) !is null ... ;
>>
>> as the number of cast(C) !is null is growing, I'm afraid of 
>> this being a inelegant or even poor performance approach. How 
>> would you do that?
>
> Isn't that what virtual function is designed for?
>
> ```
> class Base {
>   bool shouldDoX() {return false;}
> }
>
> class Derived: Base {
>   bool shouldDoX() {return true;}
> }
>
> class Derived2: Derived {
>   bool shouldDoX() {return false;}
> }
>
> ...
>
> ```

sounds a better approach, I ended up using this. Lots of cast(X), 
cast(Y), etc is probably slow and gets messy with time.


More information about the Digitalmars-d-learn mailing list