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

StackOverflow https://stackoverflow.com/questions/1820841

  •  10-07-2019
  •  | 
  •  

Question

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#?

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top