Type casting question

I need to convert one legacy C++ type to .NET type.I would like to know which is the preferred one.I am writing code in managed C++.

int a;

1)System::Int32 i = (System::Int32)a;

2)System::Int32 i = Convert::ToInt32(a);

Thanks in advance

Jimmy



Answer this question

Type casting question

  • BloomyMB

    Right, but is this static_cast is preferred over using the Convert class
  • Souhail

    Martin,

    I agree with your thoughts.

    Thanks a lot,

    Jimmy


  • Nexus_019

    Jimmy

    The C++ cast operators are available in C++/CLI and should be used over C-style casts where possible.



  • Kelly Stich

    Finally it would be the same. But why should I use a CLR feature when I have a language specific feature

    If I choose a language specific feature the compiler has much more freedom to optimize my code. If I use the CLR function I have to hope that the managed code optimiziers and the final code created by the JIT is optimized in a good way.

    I would always prefer the language features!



  • muku

    Martin,

    This actually confused me.This syntax looks like legacy C++ casting.Is this preferred in C++.NET What is your reason for using the same


  • Ananda85

    I would always use:

    System::Int32 i = static_cast<System::Int32>(a);



  • Sajid Saeed

    @Jimmy:

    C++/CLI is a language extension, all normal C++ mechanisms are still intact, this includes especially the type conversion.



  • Type casting question