Search code examples
managed-c++handle

Managed C++ ^ (handle) --> What is it and how does it relate to references and pointers


What is the difference between a handle to an object in Managed C++

such as:

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

and the ordinary C++ pointers?

Also how do they relate to references we have in C#?


Solution

  • You are not talking about (the older) Managed C++, but about C++/CLI, right?

    In C#, your code is equivalent to

    System.String str=new System.String();
    

    In C++/CLI, a handle to an object is just the same as a reference in C# - you have reference counting, garbage collector etc.

    Ordinary C++ pointers, on the other hand, are (in most cases) pointers to unmanaged objects. You can (of course) have C++ pointers to managed objects, just the way you have pointers available in C# (in unsafe code). Look here for a detailed explanation of pointers in C#, and here for some details about pointers in C++/CLI. Those pointers are not handled by the garbage collector.