Thread overview | |||||
---|---|---|---|---|---|
|
February 04, 2016 How do you check if object o has base type B? | ||||
---|---|---|---|---|
| ||||
Consider: class C { } class B : C { } class A : B { } class D : C { } C[] objList; how do we test if objLis[k] is of base type "B"? Ie for [new A(), new B(), new D(), new C()] would give output [true, true, false, false]. ? Thank you! :D |
February 04, 2016 Re: How do you check if object o has base type B? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Enjoys Math | T[] list; foreach(v; list) { if (SpecialT v2 = cast(SpecialT)v) { writeln(v2); } else { writeln("err"); } } That should work. |
February 04, 2016 Re: How do you check if object o has base type B? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Enjoys Math | On Thursday, 4 February 2016 at 05:51:22 UTC, Enjoys Math wrote:
> Consider:
>
> class C {
>
> }
>
> class B : C {
>
> }
>
> class A : B {
>
> }
>
> class D : C {
>
> }
>
> C[] objList;
>
> how do we test if objLis[k] is of base type "B"?
>
> Ie for [new A(), new B(), new D(), new C()] would give output [true, true, false, false].
>
> ?
>
> Thank you! :D
if (cast(B)objLis[k])
{
// It's an instance.
}
|
Copyright © 1999-2021 by the D Language Foundation