hr0m
hr0m

Reputation: 2805

Check class of object at runtime

I have some inheritance classes, say A and B: A. I found a way how to check at runtime, which class am i dealing with:

A a = new A;
if( some condition )
  a = new B;

if (a.classinfo.name == "a.b")
  writeln("That previous condition was met");

However is there a better way? I am really confused by now with typeid typeof and is.

Upvotes: 1

Views: 37

Answers (1)

dhasenan
dhasenan

Reputation: 1228

If you need to know if it's that type or any subtype of it, which is probably the best plan in these situations:

(cast(B)a) !is null

If you need to know an exact type, which is rare, you can use:

a.classinfo is B.classinfo

Upvotes: 2

Related Questions