Search code examples
c++-clihandles

Handle declarations


Can anyone tell me what the difference is between these 2 lines of code, which one is better to use?

    System::String ^MyStr = gcnew System::String(MyStr); 

    System::String ^MyStr; 

Solution

  • Those lines are not equivalent. In the first one, you will get an exception beacuse you're trying to create a String from an uninitialized tracking handle (MyStr). In the second one, MyStr is declared, not defined, it points to garbage and will throw an exception if you attempt to use it. Which one you should use depends on the rest of the code