Safe casting in C ++ / CLI - equivalent to C # "how to"?

I used to ask if using as in C # is safe (that is, it won't explode): Is using a "how" in C # a safe casting method?

I liked the answer and in the end I used something based on it:

 Foo x = y as Foo; if (x != null) { ... } 

But now I am converting my C # code to C ++ / CLI (due to dependency problem ..), so questions ...

I am looking for a C ++ / CLI equivalent for "how" that is safe and will not explode at run time if not the correct type. Can someone suggest a cast that suits my needs? (please provide an example if you can)

Thanks!

+7
source share
3 answers

C ++ way to do such a thing is dynamic_cast . I don't know if this is the same for managed links as unmanaged links, MSDN had very little information about this.

+12
source
+1
source

you need something called RTTI included when compiling C ++. Then you can use dynamic_cast (gives zero, similar to as ) and typeid (similar to is ).

0
source

All Articles