[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppCast

(C++) Cast, to cast

To convert one variable's data type to another. Avoid C-style casts [1-3].

  1. cast, const_cast: add or remove constness. For compatibility with non-const-correct libraries.
  2. cast, down cast: convert a base class to a derived class. Does not happen implicitly, as this will not always succeed.
  3. cast, boost::lexical_cast: convert to and from std::string.
  4. cast, dynamic_cast: convert a base class to a derived class (a down cast), or the other way around (an up cast).
  5. cast, reinterpret_cast: convert to any unrelated type, e.g. from double* to char. The 'most dirty' cast.
  6. cast, static_cast: convert between convertable built-in data types, e.g. int and double. The most common cast.
  7. cast, up cast: convert a derived class to a base class. Also happens implicitly, as this will always succeed.

Comparison between C-style casts and C++-style casts

(After a discussion at the Programmers Heaven C/C++ Message Board)

: It looks much better and less messy to use C casts...
In 'The design and evolution of C++' by Bjarne Stroustrup [4], he states that it is good that it looks messy.

A cast is not something 'normal'. If you start a function with types A and B, you expect that function to work with types A and B. A cast is something good design tries to avoid as much as possible. If it happens, it should be easily spotted. It is easy to overlook a C-style case, where a static_cast is not. Therefore, Stroustrup intentionally decided to make the syntax ugly!

Also, there are four types of C++ style casts, each with its own reason of existence: do you want to cast data type A to a compatible B (e.g. float to double), then you use a static_cast. Would A and B be the same datatype but A also has constness, use a const_cast. If A and B are related to each other ( A is a derived class of B or vice versa), use a dynamic_cast. If A and B are unrelated, use a reinterpret_cast.

These four types of casts denote four types of different information you have.

See the example below for real ambiguity:

const A * myA = /* SOMETHING */ ;
B * b = (B *) a;
// Performs a:
// - const_cast
// - static_cast (e.g. if *A is int and *B is double)
//   OR dynamic_cast (e.g. if A inherits from B)
//   OR reinterpret_cast (e.g. if *A is std::vector<int> and *B is std::auto_ptr<std::pair<int,double> >)

'Cast' links

References



last edited (January 6, 2007) by bilderbikkel, Number of views: 2474, Current Rev: 6 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.