This documentation is archived and is not being maintained.

7.3 Address-of and Static Members

Visual Studio .NET 2003

The address of a static C++ primitive type member yields a __nogc pointer.

The address of a static __value type member is a __gc pointer because __value type member is allocated on the runtime heap and can be moved by the garbage collector.

Example

// addrof_static_members.cpp
// compile with: /clr
#using <mscorlib.dll>
using namespace System;
__value struct V { int i; };

__gc struct G {
   static V v = {22}; 
   static int i = 23; 
   static String* pS = S"hello"; 
};

int main() {
   // int *p0 = &G::v.i;   // error: cannot convert int __gc* to int*   
   int __gc*p1 = &G::v.i;   // ok: value type variable may actually move
   int *p2 = &G::i;   // ok: primitive type is fixed
   String * __gc* p3 = &G::pS;   // ok
   Console::WriteLine(*p1);
   Console::WriteLine(*p2);
   Console::WriteLine(*p3);
}

Output

22
23
hello
Show: