共用方式為


如何:在 C++/CLI 中使用 safe_cast

本文說明如何在 C++/CLI 應用程式中使用safe_cast。 如需 C++/CX 中safe_cast的相關資訊,請參閱 safe_cast

向上轉型

向上轉換是從衍生型別轉換成其中一個基類。 此轉換是安全的,不需要明確的轉型標記法。 下列範例示範如何使用 和 不使用它來執行向上廣播 safe_cast

// safe_upcast.cpp
// compile with: /clr
using namespace System;
interface class A {
   void Test();
};

ref struct B : public A {
   virtual void Test() {
      Console::WriteLine("in B::Test");
   }

   void Test2() {
      Console::WriteLine("in B::Test2");
   }
};

ref struct C : public B {
   virtual void Test() override {
      Console::WriteLine("in C::Test");
   };
};

int main() {
   C ^ c = gcnew C;

   // implicit upcast
   B ^ b = c;
   b->Test();
   b->Test2();

   // upcast with safe_cast
   b = nullptr;
   b = safe_cast<B^>(c);
   b->Test();
   b->Test2();
}
in C::Test
in B::Test2
in C::Test
in B::Test2

向下轉型

向下轉換是從基類轉換成衍生自基類的類別。 只有在執行時間定址的物件實際上是定址衍生類別物件時,向下轉換才安全。 不同于 static_castsafe_cast 會執行動態檢查,並在轉換失敗時擲回 InvalidCastException

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

interface class A { void Test(); };

ref struct B : public A {
   virtual void Test() {
      Console::WriteLine("in B::Test()");
   }

   void Test2() {
      Console::WriteLine("in B::Test2()");
   }
};

ref struct C : public B {
   virtual void Test() override {
      Console::WriteLine("in C::Test()");
   }
};

interface class I {};

value struct V : public I {};

int main() {
   A^ a = gcnew C();
   a->Test();
   B^ b = safe_cast<B^>(a);
   b->Test();
   b->Test2();

   V v;
   I^ i = v;   // i boxes V
   V^ refv = safe_cast<V^>(i);

   Object^ o = gcnew B;
   A^ a2= safe_cast<A^>(o);
}
in C::Test()
in C::Test()
in B::Test2()

具有使用者定義轉換的safe_cast

下一個範例示範如何使用 來叫用 safe_cast 使用者定義的轉換。

// safe_cast_udc.cpp
// compile with: /clr
using namespace System;
value struct V;

ref struct R {
   int x;
   R() {
      x = 1;
   }

   R(int argx) {
      x = argx;
   }

   static operator R::V^(R^ r);
};

value struct V {
   int x;
   static operator R^(V& v) {
      Console::WriteLine("in operator R^(V& v)");
      R^ r = gcnew R();
      r->x = v.x;
      return r;
   }

   V(int argx) {
      x = argx;
   }
};

   R::operator V^(R^ r) {
      Console::WriteLine("in operator V^(R^ r)");
      return gcnew V(r->x);
   }

int main() {
   bool fReturnVal = false;
   V v(2);
   R^ r = safe_cast<R^>(v);   // should invoke UDC
   V^ v2 = safe_cast<V^>(r);   // should invoke UDC
}
in operator R^(V& v
in operator V^(R^ r)

safe_cast和 Boxing 作業

Box 處理

Boxing 定義為編譯器插入的使用者定義轉換。 因此,您可以使用 safe_cast 來將 CLR 堆積上的值方塊化。

下列範例顯示具有簡單和使用者定義實數值型別的 Boxing。 將 safe_cast 原生堆疊上的實數值型別變數框成方塊,以便將它指派給垃圾收集堆積上的變數。

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

interface struct I {};

value struct V : public I {
   int m_x;

   V(int i) : m_x(i) {}
};

int main() {
   // box a value type
   V v(100);
   I^ i = safe_cast<I^>(v);

   int x = 100;
   V^ refv = safe_cast<V^>(v);
   int^ refi = safe_cast<int^>(x);
}

下一個範例顯示 Boxing 在作業中 safe_cast 優先于使用者定義的轉換。

// safe_cast_boxing_2.cpp
// compile with: /clr
static bool fRetval = true;

interface struct I {};
value struct V : public I {
   int x;

   V(int argx) {
      x = argx;
   }

   static operator I^(V v) {
      fRetval = false;
      I^ pi = v;
      return pi;
   }
};

ref struct R {
   R() {}
   R(V^ pv) {}
};

int main() {
   V v(10);
   I^ pv = safe_cast<I^>(v);   // boxing will occur, not UDC "operator I^"
}

Unbox 處理

Unboxing 會定義為編譯器插入的使用者定義轉換。 因此,您可以使用 safe_cast 將 CLR 堆積上的值取消收件匣。

Unboxing 是使用者定義的轉換,但與 Boxing 不同,unboxing 必須明確,也就是說,它必須由 、C 樣式轉換或 safe_cast 執行 static_cast ;無法隱含執行 unboxing。

// safe_cast_unboxing.cpp
// compile with: /clr
int main() {
   System::Object ^ o = 42;
   int x = safe_cast<int>(o);
}

下列範例示範使用實值型別和基本型別進行 Unboxing。

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

interface struct I {};

value struct VI : public I {};

void test1() {
   Object^ o = 5;
   int x = safe_cast<Int32>(o);
}

value struct V {
   int x;
   String^ s;
};

void test2() {
   V localv;
   Object^ o = localv;
   V unboxv = safe_cast<V>(o);
}

void test3() {
   V localv;
   V^ o2 = localv;
   V unboxv2 = safe_cast<V>(o2);
}

void test4() {
   I^ refi = VI();
   VI vi  = safe_cast<VI>(refi);
}

int main() {
   test1();
   test2();
   test3();
   test4();
}

safe_cast和泛型型別

下一個範例示範如何使用 safe_cast 來執行具有泛型型別的向下轉換。

// safe_cast_generic_types.cpp
// compile with: /clr
interface struct I {};

generic<class T> where T:I
ref struct Base {
   T t;
   void test1() {}
};

generic<class T> where T:I
ref struct Derived:public Base <T> {};

ref struct R:public I {};

typedef Base<R^> GBase_R;
typedef Derived<R^> GDerived_R;

int main() {
   GBase_R^ br = gcnew GDerived_R();
   GDerived_R^ dr = safe_cast<GDerived_R^>(br);
}

另請參閱

safe_cast