Aracılığıyla paylaş


/clr ile C Türü Atamalar (C++/CLI)

Aşağıdaki konuda, yalnızca ortak dil çalışma zamanı için geçerlidir.

CLR türleri ile kullanıldığında, derleyici eşleme c stilinde aşağıdaki sıra ile aşağıda listelenen yayınları için artığını çalışır:

  1. const_cast

  2. safe_cast

  3. safe_cast artı const_cast

  4. static_cast

  5. static_cast artı const_cast

c-style cast yukarıda listelenen yayınları hiçbiri geçerli değilse ve ifadenin türü ve hedef türü CLR baþvuru türleri ise, bir çalışma zamanı denetimi için (MSIL yönergesi castclass) eşler.Aksi takdirde, c stilinde cast geçersiz olarak deðerlendirilir ve derleyici bir hataverir.

Notlar

c-style cast önerilmez.İle derlerken /CLR (ortak dil çalışma zamanı derleme), kullanma safe_cast (C++ Bileşen Uzantıları).

Aşağıdaki örnek bir c-artığını eşleyen stilini gösterir bir 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); 
}

Aşağıdaki örnek bir c-artığını eşleyen stilini gösterir bir safe_cast.

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

Aşağıdaki örnek bir c-artığını eşleyen stilini gösterir bir safe_cast + 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");
   }
}

Aşağıdaki örnek bir c-artığını eşleyen stilini gösterir bir 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;
}

Aşağıdaki örnek bir c-artığını eşleyen stilini gösterir bir static_cast + 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
}

Aşağıdaki örnek bir c-artığını eşleyen bir çalışma zamanı denetimi stilini gösterir.

// 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");
   }
}

Aşağıdaki örnek, bir geçersiz c artığını, derleyici , neden olan bir hataverecek stili göstermektedir.

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

Gereksinimler

Derleyici seçeneği:/clr

Ayrıca bkz.

Kavramlar

Çalışma platformları için bileşen uzantıları