Partager via


How to: Declare Value Types with the interior_ptr Keyword 

An interior_ptr can be used with a value type.

Example

The following sample shows how to use an interior_ptr with a value type.

// interior_ptr_value_types.cpp
// compile with: /clr
value struct V {
   V(int i) : data(i){}
   int data;
};

int main() {
   V v(1);
   System::Console::WriteLine(v.data);

   // pointing to a value type
   interior_ptr<V> pv = &v;
   pv->data = 2;

   System::Console::WriteLine(v.data);
   System::Console::WriteLine(pv->data);

   // pointing into a value type
   interior_ptr<int> pi = &v.data;
   *pi = 3;
   System::Console::WriteLine(*pi);
   System::Console::WriteLine(v.data);
   System::Console::WriteLine(pv->data);
}

Output

1
2
2
3
3
3

Example

In a value type, the this pointer evaluates to an interior_ptr.

In the body of a non-static member-function of a value type V, this is an expression of type interior_ptr<V> whose value is the address of the object for which the function is called.

// interior_ptr_value_types_this.cpp
// compile with: /clr /LD
value struct V {
   int data;
   void f() {
      interior_ptr<V> pv1 = this;
      // V* pv2 = this;   error
   }
};

Example

The following sample shows how to use the address-of operator with static members.

The address of a static C++ type member yields a native pointer. The address of a static value type member is a managed pointer because value type member is allocated on the runtime heap and can be moved by the garbage collector.

// interior_ptr_value_static.cpp
// compile with: /clr
using namespace System;
value struct V { int i; };

ref struct G {
   static V v = {22}; 
   static int i = 23; 
   static String^ pS = "hello"; 
};

int main() {
   interior_ptr<int> p1 = &G::v.i;
   Console::WriteLine(*p1);

   interior_ptr<int> p2 = &G::i;
   Console::WriteLine(*p2);

   interior_ptr<String^> p3 = &G::pS;
   Console::WriteLine(*p3);
}

Output

22
23
hello

See Also

Reference

interior_ptr