Cast di tipo C con /clr (C++/CLI)
Nell'argomento si applica solo a Common Language Runtime.
Una volta utilizzato con i tipi CLR, il compilatore tenta di associare il cast di tipo C a uno dei cast elencati di seguito, nell'ordine seguente:
const_cast
safe_cast
safe_cast più const_cast
static_cast
static_cast più const_cast
Se nessuno dei cast elencati in precedenza è valido e se il tipo dell'espressione e il tipo di destinazione sono tipi di riferimento CLR, mappe di cast di tipo C al controllo a runtime (istruzione MSIL di castclass). In caso contrario, un cast di tipo C è considerato non valido e il compilatore segnala un errore.
Note
Un cast di tipo C non è consigliato. Quando si compila con /clr (Compilazione Common Language Runtime), utilizzare safe_cast (Estensioni del componente C++).
L'esempio seguente mostra un cast di tipo C che esegue il mapping ad un 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);
}
L'esempio seguente mostra un cast di tipo C che esegue il mapping ad un safe_cast.
// cstyle_casts_2.cpp
// compile with: /clr
using namespace System;
int main() {
Object ^ o = "hello";
String ^ s = (String^)o;
}
L'esempio seguente mostra un cast di tipo C che esegue il mapping ad un safe_cast più const_cast..
// 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");
}
}
L'esempio seguente mostra un cast di tipo C che esegue il mapping ad un 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;
}
L'esempio seguente mostra un cast di tipo C che esegue il mapping ad un static_cast più const_cast..
// 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
}
L'esempio seguente mostra un cast di tipo C che esegue il mapping a un controllo in fase di esecuzione.
// 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");
}
}
L'esempio seguente mostra un cast di tipo C non valido, il quale causa un errore dal compilatore.
// cstyle_casts_7.cpp
// compile with: /clr
using namespace System;
int main() {
String^s = S"hello";
int i = (int)s; // C2440
}
Requisiti
Opzione del compilatore: /clr