boxing(C++/CLI 및 C++/CX)
값 형식을 개체로 변환하는 작업을 boxing이라고 하고, 개체를 값 형식으로 변환하는 작업을 unboxing이라고 합니다.
모든 런타임
(이 언어 기능에는 모든 런타임에 적용되는 설명이 없습니다.)
Windows 런타임
C++/CX에서는 boxing 값 형식과 unboxing 참조 형식에 대한 간단한 구문을 지원합니다. 값 형식은 형식 Object
의 변수에 할당될 때 boxing됩니다. Object
변수는 값 형식 변수에 할당되고 unboxing된 형식이 괄호 안에 지정될 때, 즉 개체 변수가 값 형식으로 캐스팅될 때 unboxing됩니다.
Platform::Object^
object_variable = value_variable;
value_variable = (value_type) object_variable;
요구 사항
컴파일러 옵션: /ZW
예제
다음 코드 예제에서는 DateTime
값을 boxing 및 unboxing합니다. 먼저 예제에서는 현재 날짜 및 시간을 나타내는 DateTime
값을 가져와 DateTime
변수에 할당합니다. 그런 다음, DateTime
을 Object
변수에 할당하여 boxing합니다. 마지막으로 boxed 값을 다른 DateTime
변수에 할당하여 unboxing합니다.
예제를 테스트하려면 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)을 참조하세요.
공용 언어 런타임
컴파일러는 값 형식을 Object로 boxing합니다. 값 형식을 Object로 변환하는 컴파일러에서 정의된 변환 때문에 이 작업이 가능합니다.
Boxing 및 unboxing을 통해 값 형식으로 개체로 처리할 수 있습니다. 구조체 형식 및 int와 같은 기본 제공 형식을 비롯하여 값 형식을 Object 형식으로 변환하거나 그 반대로 변환할 수 있습니다.
자세한 내용은 다음을 참조하세요.
요구 사항
컴파일러 옵션: /clr
예제
다음 예제에서는 암시적 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^)
}
1
xx = 10
in A
Class V passed using implicit boxing
Class V passed with forced boxing
Interface function
in func1(V2^)
in func2(System::ValueType^)
in func2(System::ValueType^)
참고 항목
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기