Thread: type casting

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    type casting

    In the Deitel book it tells me to cast as follows:

    static_cast< double >( operand )

    Yet in the Schildt book it does the following:

    (double)operand

    What is the difference, and which one is better to use?

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Static_cast is a C++ style of casting. It's a restricted version of its C counterpart; it will only convert between related pointer types. It is possible to cast pointers up and down the inheritance hierarchy, but not to a type outside of the hierarchy. Because of that checking feature, static_cast is the better one. (C++ for game programmers )
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> it will only convert between related pointer types

    Not quite. It'll convert between related types: pointers within the same class hierarchy, integral and enum types, floating point and integral types, etc.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > In the Deitel book it tells me to cast as follows:
    > static_cast< double >( operand )
    No doubt it will go on to explain about dynamic_cast, const_cast etc...
    This is very much the preferred way in new C++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What dose this type casting mean??
    By zhoufanking in forum C Programming
    Replies: 4
    Last Post: 06-11-2008, 06:09 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. help with simple type casting problem
    By Jeremy_S in forum C Programming
    Replies: 2
    Last Post: 02-27-2002, 12:38 PM