다음을 통해 공유


방법: interior_ptr 키워드를 사용하여 값 형식 선언(C++/CLI)

interior_ptr은 값 형식에 사용할 수 있습니다.

Important

이 언어 기능은 /clr 컴파일러 옵션에서 지원하지만 /ZW 컴파일러 옵션에서는 지원하지 않습니다.

예: 값 형식의 interior_ptr

설명

다음 C++/CLI 샘플에서는 값 형식에 interior_ptr을 사용하는 방법을 보여 줍니다.

코드

// 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);
}
1
2
2
3
3
3

예: this 포인터

설명

값 형식에서 포인터는 this interior_ptr 계산됩니다.

값 형식 Vthis 의 비정적 멤버 함수 본문에서 값이 함수가 호출되는 개체의 주소인 형식 interior_ptr<V> 의 식입니다.

코드

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

예: address-of 연산자

설명

다음 샘플은 정적 멤버에 주소 연산자를 사용하는 방법을 보여 줍니다.

정적 Visual C++ 형식 멤버의 주소는 네이티브 포인터를 생성합니다. 정적 값 형식 멤버의 주소는 값 형식 멤버가 런타임 힙에 할당되고 가비지 수집기에서 이동할 수 있기 때문에 관리되는 포인터입니다.

코드

// 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);
}
22
23
hello

참고 항목

interior_ptr(C++/CLI)