[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppCast
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:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppCast
(C++) Cast, to cast
To convert one variable's data type to another. Avoid C-style casts [1-3].- cast, const_cast: add or remove constness. For compatibility with non-const-correct libraries.
- cast, down cast: convert a base class to a derived class. Does not happen implicitly, as this will not always succeed.
- cast, boost::lexical_cast: convert to and from std::string.
- cast, dynamic_cast: convert a base class to a derived class (a down cast), or the other way around (an up cast).
- cast, reinterpret_cast: convert to any unrelated type, e.g. from double* to char. The 'most dirty' cast.
- cast, static_cast: convert between convertable built-in data types, e.g. int and double. The most common cast.
- 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
- 1) Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter 6.5.4: 'Avoid explicit type conversion (casts)'.
- 2) Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4.Chapter 6.5.5: 'When explicit type conversion is necessary, prefer the more specific cast operators to the C-style cast'.
- 3) 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'
- 4) Bjarne Stroustrup. The design and evolution of C++.ISBN: 0-201-54330-3
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
