它是什么以及它与引用和指针有何关系

它是什么以及它与引用和指针有何关系

本文介绍了托管C ++ ^(句柄)->它是什么以及它与引用和指针有何关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

托管C ++中的对象的句柄之间有什么区别

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

例如:

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

和普通的C ++指针?

and the ordinary C++ pointers?

它们还与C#中的引用有何关系?

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

推荐答案

您不是在谈论(较旧的)托管C ++,而是在谈论C ++/CLI,对吧?

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

在C#中,您的代码等效于

In C#, your code is equivalent to

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

在C ++/CLI中,对象的句柄与C#中的引用完全相同-您具有引用计数,垃圾收集器等.

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

另一方面,普通C ++指针在大多数情况下是指向非托管对象的指针.您可以(当然)拥有指向托管对象的C ++指针,就像拥有C#中可用的指针的方式一样(不安全的代码).在此处中,可以找到有关C#中指针的详细说明,以及此处了解有关C ++/CLI中的指针的一些详细信息.这些指针不由垃圾收集器处理.

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.

这篇关于托管C ++ ^(句柄)->它是什么以及它与引用和指针有何关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 01:13