Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Cast

 

A cast is used to convert one data type to another. Don't use C-style casts, but use one of the four C++ casting keywords [1][2].

 

There are four keywords for the different types of casts:

  1. static_cast: to cast related types, for example int to double
  2. dynamic_cast: to cast between types in an inheritance hierarchy, for example from the base class ChessPiece to its derived class Pawn
  3. const_cast
  4. reinterpret_cast

 

 

 

 

 

References

 

  1. Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4 6.5.5: 'When explicit type conversion is necessary, prefer the more specific cast operators to the C-style cast'.
  2. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 95: 'Don't use C-style casts'.

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict