__gc

备注

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

声明 __gc 类型。

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

备注

__gc 类型是 C++ 语言扩展,它通过提供互操作性和垃圾回收等功能来简化 .NET Framework 编程。

备注

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

在 C++ 托管扩展中,C# 类和 C# 结构的等效项如下所示:

C++ 托管扩展

C#

更多相关信息

__gc 结构或 __gc 类

class 关键字

__value 结构或 __value 类

struct

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);
}

输出

0
4
5