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

久未见 提交于 2019-12-13 02:01:27

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/1820841/managed-c-handle-what-is-it-and-how-does-it-relate-to-references-and-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!