typecasting

This is a discussion on typecasting within the C++ Programming forums, part of the General Programming Boards category; ive seen typecasting done this way: Code: int theInt=5; char theChar; theChar=(char) theInt; but as far as i can tell ...

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    typecasting

    ive seen typecasting done this way:
    Code:
    int theInt=5;
    char theChar;
    theChar=(char) theInt;
    but as far as i can tell this would also be typecasting:
    Code:
    int theInt=5;
    char theChar;
    theChar=int (theInt);
    is one used in specific cases, or can it work both ways at any time? or is there a flaw in my logic?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Of course, 'theChar=int (theInt);' would be improper typecasting, it should be char (theInt).

    But, its fine both ways (I think).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    oh oops, heh, i always make stupid mistakes like that...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by XSquared
    But, its fine both ways (I think).
    Yep... Either one works.
    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.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    C++ supports two "bad" forms of casting: the C style cast
    Code:
    x = (type)y;
    and the function style cast
    Code:
    x = type(y);
    Both are interchangeable in most cases, but both are considered bad style for C++, which shuns casting in general and offers cast operators that are easier to find and more annoying, thus less used (sneaky, no?):
    Code:
    x = static_cast<type>(y);
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    I usually do:

    Code:
    average = (double)sumOfValues /  (double)amountOfValues;
    or something like that. It works fine, I never tryed doing it the other way...

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    average = static_cast<double>(sumOfValues) / static_cast<double>(amountOfValues);

    ... That just seems like too much exercise for the keyboard fingers (despite the fact that it is technically better).
    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.

  8. #8
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Could someone please tell me way is best to do the C++ casting? Itīs because the C++ has a better type checking?
    Nothing more to tell about me...
    Happy day =)

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Could someone please tell me way is best to do the C++ casting?
    The best way is not to do it at all if you can possibly avoid it.
    My best code is written with the delete key.

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Assuming you can't avoid it - which is rare when not converting from floating point types to integers - its fine to use the old way, though when working with more complex types, and conversions between derived and base classes and such, its better to use the C++ operators.
    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.

  11. #11
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    What about data structures (like a stack) that store generic data (like void* pointers), which (for example) allows you to store heterogenous lists of items? In order to do anything useful, you'll have to cast whatever you retrieve from the structure to whatever type it really is (and you'd have to know what that is). If casting is so frowned upon, there must be a better way to design such a structure, and I'm curious as to what that is.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >If casting is so frowned upon, there must be a better way to design such a structure, and I'm curious as to what that is.
    Templates are far better than void*'s for implementing generic data structures.
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Originally posted by Prelude
    >If casting is so frowned upon, there must be a better way to design such a structure, and I'm curious as to what that is.
    Templates are far better than void*'s for implementing generic data structures.
    But would they let you store a variety of different objects? For example, std::map is a template that lets you store a list of any kind of object, as long as each one is the same. What if you wanted to store a list of floats and chars (strange example, but I'm trying to stick to basic types)?

  14. #14
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    For what you're talking about, you might want to look into unions.

    Though, in general keeping an array of objects of different type (void*'s if you will) is not particularly useful since you have to keep track of what they are somewhere, and you can get into some really messy RTTI stuff.

    If you need multiple types of objects in a structure, you might want to consider deriving them all from a common base class.
    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.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But would they let you store a variety of different objects?
    A heterogeneous data structure is what you're talking about. Generally, keeping a data structure of completely different types isn't as useful as you might think. The bookkeeping is tedious and the gain superficial. However, you can use inheritance and polymorphism to create a data structure of abstract base class pointers and use those pointers to access derived objects of different types. Such structures of similar types are very useful, and the language supports them easily.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer Arithmetic and Typecasting
    By pobri19 in forum C Programming
    Replies: 2
    Last Post: 03-19-2009, 11:06 PM
  2. recv() typecasting error
    By jmd15 in forum Networking/Device Communication
    Replies: 7
    Last Post: 05-19-2006, 03:32 PM
  3. c and c++ style typecasting confusion
    By vaibhav in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2005, 07:29 AM
  4. typecasting
    By sreetvert83 in forum C++ Programming
    Replies: 7
    Last Post: 07-22-2005, 01:55 PM
  5. Typecasting a void* to a function pointer
    By Procyon in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2004, 04:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21