__gc

备注

本主题仅适用于托管扩展的 1 版 C++ 的。只应在此语法维护 1 版代码。有关使用等效功能的信息在新语法,请参见 类和结构 (托管)

声明 __gc 类型。

__gc array-specifier
__gc class-specifier
__gc struct-specifier
__gc interface-specifier
__gc pointer-specifier
__gc new

备注

__gc 类型是 c. C++ 简化编程通过提供的 .NET framework 功能比如互操作性和垃圾回收的语言扩展。

备注

__gc 抽象类的每个成员函数,除非成员函数是纯虚函数,必须定义。

在 C++ 托管扩展中,对于 C# 类的等效和 C# struct 如下所示:

C++ 托管扩展

C#

更多信息

__gc 结构或 __gc 类

class

关键字

__value 结构或 __value 类

struct

结构 关键字

示例

在下面的示例中,托管类 (X) 声明了一个公共数据成员,通过托管指针进行操作:

// keyword__gc.cpp
// compile with: /clr:oldSyntax
#using <mscorlib.dll>
using namespace System;

__gc class X {
public:
   int i;
   int ReturnInt() { return 5; }
};

int main() {
   // X is a __gc class, so px is a __gc pointer
   X* px;
   px = new X;   // creates a managed object of type X
   Console::WriteLine(px->i);

   px->i = 4;   // modifies X::i through px
   Console::WriteLine(px->i);

   int n = px->ReturnInt();   // calls X::ReturnInt through px
   Console::WriteLine(n);
}

Output

0
4
5