다음을 통해 공유


Boxing(C++ 구성 요소 확장)

Visual C++ 컴파일러는 boxing이라는 프로세스에서 값 형식을 개체로 변환하고 unboxing이라는 프로세스에서 개체를 값 형식으로 변환할 수 있습니다.

모든 런타임

(모든 런타임에 적용되는 이 언어 기능에 대한 설명이 없습니다.)

Windows 런타임

C++/CX에서는 boxing 값 형식과 unboxing 참조 형식에 대한 약식 구문을 지원합니다.Object 형식의 변수에 할당되면 값 형식은 boxed 형식입니다.Object 변수는 값 형식 변수로 할당되고 unboxed 형식을 괄호 안에 지정할 때, 즉, 개체 변수가 값 형식으로 캐스팅될 때 unboxed됩니다.

    Platform::Object^ object_variable  = value_variable;
    value_variable = (value_type) object_variable;

c53ss7ze.collapse_all(ko-kr,VS.110).gif요구 사항

컴파일러 옵션: /ZW

c53ss7ze.collapse_all(ko-kr,VS.110).gif예제

다음 코드 예제에서는 DateTime 값을 box 및 unbox합니다.우선 이 예제에서는 현재 날짜와 시간을 나타내는 DateTime 값을 가져와서 DateTime 변수에 할당합니다.그런 다음 개체 변수에 할당하여 DateTime이 boxing됩니다.마지막으로 boxed 값은 다른 DateTime 변수에 할당하여 unboxed됩니다.

예제를 테스트하려면 BlankApplication 프로젝트를 만들고 BlankPage::OnNavigatedTo() 메서드를 바꾼 다음 닫는 괄호에 중단점을 지정하고 변수 str1을 할당합니다.예제가 닫는 대괄호에 이르면 str1을 검사합니다.

void BlankPage::OnNavigatedTo(NavigationEventArgs^ e)
{
    using namespace Windows::Globalization::DateTimeFormatting;

    Windows::Foundation::DateTime dt, dtAnother;
    Platform::Object^ obj1;

    Windows::Globalization::Calendar^ c = 
        ref new Windows::Globalization::Calendar;
    c->SetToNow();
    dt = c->GetDateTime();
    auto dtf = ref new DateTimeFormatter(
                           YearFormat::Full, 
                           MonthFormat::Numeric, 
                           DayFormat::Default, 
                           DayOfWeekFormat::None);
    String^ str1 = dtf->Format(dt);
    OutputDebugString(str1->Data());
    OutputDebugString(L"\r\n");

    // Box the value type and assign to a reference type.
    obj1 = dt;
    // Unbox the reference type and assign to a value type.
    dtAnother = (Windows::Foundation::DateTime) obj1;

    // Format the DateTime for display.
    String^ str2 = dtf->Format(dtAnother);
    OutputDebugString(str2->Data());
}

자세한 내용은 Boxing(C++/CX)을 참조하십시오.

공용 언어 런타임

Visual C++ 컴파일러는 이제 값 형식을 Object로 boxing합니다.이는 값 형식을 Object로 변환하는 컴파일러에서 정의된 변환으로 인해 가능합니다.

boxing과 unboxing을 사용하여 값 형식이 개체로 처리되도록 할 수 있습니다.int 같은 구조체 형식과 기본 제공 형식을 포함한 값 형식은 형식 Object 간에 변환할 수 있습니다.

자세한 내용은 다음을 참조하십시오.

c53ss7ze.collapse_all(ko-kr,VS.110).gif요구 사항

컴파일러 옵션: /clr

c53ss7ze.collapse_all(ko-kr,VS.110).gif예제

예제

다음 샘플에서는 암시적 boxing이 작동하는 방법을 보여줍니다.

// vcmcppv2_explicit_boxing2.cpp
// compile with: /clr
using namespace System;

ref class A {
public:
   void func(System::Object^ o){Console::WriteLine("in A");}
};

value class V {};

interface struct IFace {
   void func();
};

value class V1 : public IFace {
public:
   virtual void func() {
      Console::WriteLine("Interface function");
   }
};

value struct V2 {
   // conversion operator to System::Object
   static operator System::Object^(V2 v2) {
      Console::WriteLine("operator System::Object^");
      return (V2^)v2;
   }
};

void func1(System::Object^){Console::WriteLine("in void func1(System::Object^)");}
void func1(V2^){Console::WriteLine("in func1(V2^)");}

void func2(System::ValueType^){Console::WriteLine("in func2(System::ValueType^)");}
void func2(System::Object^){Console::WriteLine("in func2(System::Object^)");}

int main() {
   // example 1 simple implicit boxing
   Int32^ bi = 1;
   Console::WriteLine(bi);

   // example 2 calling a member with implicit boxing
   Int32 n = 10;
   Console::WriteLine("xx = {0}", n.ToString());

   // example 3 implicit boxing for function calls
   A^ a = gcnew A;
   a->func(n);

   // example 4 implicit boxing for WriteLine function call
   V v;
   Console::WriteLine("Class {0} passed using implicit boxing", v);
   Console::WriteLine("Class {0} passed with forced boxing", (V^)(v));   // force boxing

   // example 5 casting to a base with implicit boxing
   V1 v1;
   IFace ^ iface = v1;
   iface->func();

   // example 6 user-defined conversion preferred over implicit boxing for function-call parameter matching
   V2 v2;
   func1(v2);   // user defined conversion from V2 to System::Object preferred over implicit boxing
                // Will call void func1(System::Object^);

   func2(v2);   // OK: Calls "static V2::operator System::Object^(V2 v2)"
   func2((V2^)v2);   // Using explicit boxing: calls func2(System::ValueType^)
}

Output

  
  
  
  
  
  
  
  
  

참고 항목

개념

런타임 플랫폼의 구성 요소 확장