gcnew 和 ref new 有什么区别

What is the difference between gcnew and ref new

我刚刚偶然发现 ref new here,我想知道 gcnew 是否有区别。

代码示例 here 显示了如何使用 ref new 创建托管堆上对象的句柄:

Foo^ spFoo = ref new Foo();

并使用 gcnew here

 int ^ i = gcnew int(13);

那么它们在概念上是等价的吗?

如果您只是对文档的布局感到困惑:

C++/CLI 用于公共语言运行时 (CLR)。

C++/CX 适用于 Windows 运行时 (WinRT)。

它们非常相似,可以一起记录,但它们并不相同。

这就是为什么他们在您链接的文档中有三个标题:

  • 所有平台
  • Windows 运行时间
  • 公共语言运行时

对于 C++/CLI,gcnew 是您使用的。除非您使用的是 C++/CX,否则 "Windows Runtime" 部分不适用。