Umwandlungen im C-Stil mit /clr (C++/CLI)

Das folgende Thema gilt nur für die Common Language Runtime.

Bei Verwendung mit CLR-Typen versucht der Compiler C-stilartige Umwandlungen einer der unten aufgeführten Umwandlungen zuzuordnen, und zwar in der folgenden Reihenfolge:

  1. const_cast

  2. safe_cast

  3. safe_cast plus const_cast

  4. static_cast

  5. static_cast plus const_cast

Wenn keine der oben aufgeführten Umwandlungen gültig ist und der Typ des Ausdrucks sowie der Zieltyp CLR-Verweistypen sind, wird eine C-stilartige Umwandlung (Castclass-MSIL-Anweisung) einer Laufzeitüberprüfung zugeordnet. Andernfalls wird eine C-stilartige Umwandlung als ungültig betrachtet, und der Compiler gibt einen Fehler aus.

Hinweise

Eine C-stilartige Umwandlung wird nicht empfohlen. Verwenden Sie beim Kompilieren mit /clr (Common Language Runtime Compilation)safe_cast.

Das folgende Beispiel zeigt eine C-Format-Umwandlung, die einem const_cast.

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

ref struct R {};
int main() {
   const R^ constrefR = gcnew R();
   R^ nonconstR = (R^)(constrefR);
}

Das folgende Beispiel zeigt eine C-stilartige Umwandlung, die zu safe_cast zuordnet.

// cstyle_casts_2.cpp
// compile with: /clr
using namespace System;
int main() {
   Object ^ o = "hello";
   String ^ s = (String^)o;
}

Das folgende Beispiel zeigt eine C-Format-Umwandlung, die einem safe_cast plus const_castzugeordnet ist.

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

ref struct R {};
ref struct R2 : public R {};

int main() {
   const R^ constR2 = gcnew R2();
   try {
   R2^ b2DR = (R2^)(constR2);
   }
   catch(InvalidCastException^ e) {
      System::Console::WriteLine("Invalid Exception");
   }
}

Das folgende Beispiel zeigt eine C-Format-Umwandlung, die einem static_cast.

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

struct N1 {};
struct N2 {
   operator N1() {
      return N1();
   }
};

int main() {
   N2 n2;
   N1 n1 ;
   n1 = (N1)n2;
}

Das folgende Beispiel zeigt eine C-Format-Umwandlung, die einem static_cast Pluszeichen const_castzugeordnet ist.

// cstyle_casts_5.cpp
// compile with: /clr
using namespace System;
struct N1 {};

struct N2 {
   operator const N1*() {
      static const N1 n1;
      return &n1;
   }
};

int main() {
   N2 n2;
   N1* n1 = (N1*)(const N1*)n2;   // const_cast + static_cast
}

Das folgende Beispiel zeigt eine C-stilartige Umwandlung, die zu einer Laufzeitüberprüfung zuordnet.

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

ref class R1 {};
ref class R2 {};

int main() {
   R1^ r  = gcnew R1();
   try {
      R2^ rr = ( R2^)(r);
   }
   catch(System::InvalidCastException^ e) {
      Console::WriteLine("Caught expected exception");
   }
}

Das folgende Beispiel zeigt eine ungültige C-stilartige Umwandlung, die dazu führt, dass der Compiler einen Fehler ausgibt.

// cstyle_casts_7.cpp
// compile with: /clr
using namespace System;
int main() {
   String^s = S"hello";
   int i = (int)s;   // C2440
}

Anforderungen

Compileroption: /clr

Siehe auch

Komponentenerweiterungen für .NET und UWP