Boxing (C++ 元件擴充功能)

Visual C++ 編譯器可以使用稱為「Boxing」的程序將實值型別轉換成物件,並使用稱為「Unboxing」的程序將物件轉換成實值型別。

所有執行階段

(這個語言功能沒有適用於所有執行階段的備註)。

Windows 執行階段

C++/CX 支援 Boxed 實值型別和 Unboxing 參考型別的速記語法。當實值型別是指派給型別為 Object 的變數時,這個型別已進行 Box 處理。將 Object 變數指派給實值型別變數,而且在括號內指定 Unboxed 型別時 (也就是,當物件變數轉換成實值型別時),這個變數會進行 Unboxing 處理。

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

c53ss7ze.collapse_all(zh-tw,VS.110).gif需求

編譯器選項:/ZW

c53ss7ze.collapse_all(zh-tw,VS.110).gif範例

下列程式碼範例會針對 DateTime 設定 Boxed 和 Unboxed 值。首先,範例會取得表示目前日期和時間的 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)

Common Language Runtime

Visual C++ 編譯器立即以 Boxing 方式將實值型別處理成 Object。因為有可將實值型別轉換為 Object 的編譯器定義轉換,所以這是可行的。

Boxing 和 Unboxing 可讓實值型別當做物件來處理。實值型別 (包括結構型別和內建型別,例如 int) 可以和型別 Object 往來進行轉換。

如需詳細資訊,請參閱:

c53ss7ze.collapse_all(zh-tw,VS.110).gif需求

編譯器選項:/clr

c53ss7ze.collapse_all(zh-tw,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

  
  
  
  
  
  
  
  
  

請參閱

概念

執行階段平台的元件擴充功能