gcnew是什么?
我偶然发现了这段代码,我很自豪无法去询问作者它的含义。
Hashtable^ tempHash = gcnew Hashtable(iterators_);
IDictionaryEnumerator^ enumerator = tempHash->GetEnumerator();
什么是 gcnew
以及使用它而不是简单的 new
有多重要? (我也被插入符号难住了;我问过在这里。)
I stumbled across this code and am too proud to go and ask the author what it means.
Hashtable^ tempHash = gcnew Hashtable(iterators_);
IDictionaryEnumerator^ enumerator = tempHash->GetEnumerator();
What is gcnew
and how important is it to use that instead of simply new
? (I'm also stumped by the caret; I asked about that over here.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
gcnew 适用于 .NET 引用对象; 使用 gcnew 创建的对象会自动被垃圾回收; 将 gcnew 与 CLR 类型一起使用非常重要
gcnew is for .NET reference objects; objects created with gcnew are automatically garbage-collected; it is important to use gcnew with CLR types
gcnew 是一个用于分配内存的运算符,就像 new 运算符一样,只不过您不需要删除用它创建的任何内容; 这是g套利c收集的。 您可以使用
gcnew
创建 .Net 托管类型,使用new
创建非托管类型。gcnew
is an operator for allocating memory, just like thenew
operator, except you don't need todelete
anything created with it; it's garbage collected. You usegcnew
for creating .Net managed types, andnew
for creating unmanaged types.声明类型时,脱字号
^
的作用类似于 C/C++ 中的*
:在描述托管对象时,我使用术语“指针”,因为托管对象可以是与
nullptr
相比,就像 C/C++ 中的指针一样。 C/C++ 中的引用不能与 nullptr 进行比较,因为它是现有对象的地址。托管对象使用自动引用计数,这意味着当它们的引用计数为零时,它们会被自动销毁(尽管如果两个或多个无法访问的对象相互引用,您仍然会出现内存泄漏。)请注意,自动引用计数不是免费的性能明智的,所以明智地使用它。
The caret
^
acts similarly to the*
in C/C++ when declaring a type:I use the term 'pointer' when describing the managed object, as a managed object can be compared to
nullptr
just like a pointer in C/C++. A reference in C/C++ can not be compared tonullptr
as it is the address of an existing object.Managed objects use automatic-reference-counting meaning that they are destroyed automatically when they have a reference count of zero (although if two or more unreachable objects refer to each other, you will still have a memory leak.) Be warned that automatic reference counting is not free performance wise, so use it wisely.