Pages

July 25, 2010

New Style Casts

New Style Casts

The new style casts are both more specific and much easier to spot. Their syntax includes the letters _cast, which makes searching more straightforward. The casts are in template function form, where the variable or object to be cast is passed as the function argument and the destination type is passed as the template argument. There are four distinct categories of casts:

  • static cast
    static_cast( minutes )
  • const cast
    const_cast(&p)(Polygon is a class here)
  • re-interpret cast
  • Re-Interpret Cast

    A re-interpret cast converts a value between unrelated data types such asinteger to pointer type, pointer to unrelated pointer type

For example,


 // Re-Interpret Cast
// reinterpret.cpp
// Nov 21 2005

#include
using namespace std;

int main( ) {
int* p;
int x = 0x00ff00;

p = reinterpret_cast( x ); // New Style Cast
cout << hex << p << endl;

return 0;
}
  • dynamic cast

No comments:

Post a Comment