We recommend using Visual Studio 2017


Note Note

This topic applies only to version 1 of Managed Extensions for C++. This syntax should only be used to maintain version 1 code. See Classes and Structs (C++ Component Extensions) for information on using the equivalent functionality in the new syntax.

Declares a __gc type.

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

A __gc type is a C++ language extension that simplifies .NET Framework programming by providing features such as interoperability and garbage collection.

Note Note

Every member function of an abstract __gc class must be defined unless the member function is pure virtual.

In Managed Extensions for C++, the equivalents to a C# class and a C# struct are as follows:

Managed Extensions for C++


For more information

__gc struct or __gc class


class keyword

__value struct or __value class


struct keyword

In the following example, a managed class (X) is declared with a public data member, which is manipulated through a managed pointer:

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

__gc class X {
   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

   px->i = 4;   // modifies X::i through px

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