Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
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:
- static_cast: to cast related types, for example int to double
- dynamic_cast: to cast between types in an inheritance hierarchy, for example from the base class ChessPiece to its derived class Pawn
- const_cast
- reinterpret_cast
- 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'.
- 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.
