Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

Compiler Warning (level 1) C4303

Error Message

'cast' from 'type1' to 'type2' is deprecated, use static_cast, __try_cast or dynamic_cast

C-style type casting or function-style casting is not supported when using Managed Extensions for C++. To cast, use either dynamic_cast Operator or static_cast Operator.

C4303 is not reachable using /clr and the new syntax; see C-Style Casts with /clr for more information.

The following sample generates C4303:

// C4303.cpp
// compile with: /clr:oldSyntax /W1
__gc struct A { };
__gc struct B { };

int main() {
   B *b = new B;
   try {

   // C4303 old 'c' style cast
   A *a = (A*)b;   // C4303
   // run time exception becaused casting to incompatible types

   // try the following line instead
   // A *a = __try_cast<A*>(b);
   } 

   catch (System::InvalidCastException * e) {
      System::Console::WriteLine(e);
   }
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.