static_cast vs cli::safe_cast
Managed C++/CLI
2
Posts
2
Posters
0
Views
1
Watching
-
Hi! I want the advice of the community on using usual static_cast instead of cli::safe_cast. Which one is preferable?
-
Hi! I want the advice of the community on using usual static_cast instead of cli::safe_cast. Which one is preferable?
As far as I know, safe_cast is more like dynamic_cast (which you can also use on managed types). safe_cast and dynamic_cast are for casting between handles to polymorphic types within a class hierarchy (much as you would do in C++ with pointers). The difference is that safe_cast throws an exception when the cast is not possible rather than returning nullptr. This means you don't have to write code that checks to see whether the cast succeded, just handle the exception, meaning potentially less cluttered code.