开发者

Safe casting in C++/CLI - equivalent to C#'s "as"?

Earlier I asked if using as in C# was safe (i.e. won't blow up): Is using "as" in C# a safe way of casting?

I liked the answer and ended up using something based off of it:

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

But now I am converting my C# code to C++/CLI (due to a dependance issue..) so questions is..

I'm looking for the C++/CLI equivalent to "as", that is safe and won't blow up at runtime if not correct type. Can anyone 开发者_运维百科suggest a cast that meets my needs? (please provide an example if you can)

Thanks!


The C++ way to do such a thing is dynamic_cast. I don't know if it's the same for managed references as unmanaged references, MSDN had very little information on it.


This may be helpful: http://msdn.microsoft.com/en-us/library/85af44e9


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜