Thread: Am I using casts properly?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    26

    Am I using casts properly?

    There are some pretty big gaps in my programming knowledge, one of them being casts. I was writing code like this:

    Code:
    int a, c;
    float b;
    a = 100;
    b = 0.8;
    c = a*b;
    My compiler threw a warning, which I was kind of expecting. I switched it to:

    Code:
    int a, c;
    float b;
    a = 100;
    b = 0.8;
    c = static_cast<int> (a*b);
    And that seemed to correct it, I just wanted to know if I'm using casts the right way. I got a little confused reading about the different kinds of casts. Also, should I actually put b=0.8f? Not sure since I already declared it as a float.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dondrei
    And that seemed to correct it, I just wanted to know if I'm using casts the right way. I got a little confused reading about the different kinds of casts.
    Yes, static_cast should be the correct cast to use, and if you really do want to truncate the result of (a * b), then it is also applied correctly.

    Quote Originally Posted by Dondrei
    Also, should I actually put b=0.8f? Not sure since I already declared it as a float.
    Technically, 0.8 is a double literal, so if you want to be pedantically correct, you should use 0.8f.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The compiler can easily truncate 0.8 to a float, but if you use higher numbers... then you may get a truncation warning. You may just get it anyway, so using float constants is best.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Cool, thanks.

    What are the other kinds of casts used for? Pointers or something?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    const_cast<> - to remove constness or add/remove volatile. Use with EXTREME care.
    dynamic_cast<> - convert pointers from one class to other related classes (that is, "move within the inheritance ladder).
    reinterpret_cast<> - convert any bit pattern to the same bit pattern in a different type, e.g integer to pointer, pointer of one type to a different type of pointer (e.g. void * to a class pointer). Again, use with care, as it's YOUR fault if what you do doesn't work (or doesn't ALWAYS work).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Oh, that makes sense.

    Why would you want to de-const a const? Doesn't that defeat the purpose?

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Dondrei View Post
    Oh, that makes sense.

    Why would you want to de-const a const? Doesn't that defeat the purpose?
    I've used a lot of badly written code which takes non-const parameters, but it doesn't actually modify those parameters; so if I pass a const parameter, I need to de-constify it first, otherwise it won't compile.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    I have to admit that I've written some code like that before until I understood the point of using const's (wow... that's a wierd pronounciation).

    I've only ever used it in the case of casting a const char* to a char*. Then I remembered that I was using C++ and that STL isn't evil so that problem was solved...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 03:14 AM
  2. C++ system("rm") not deleting properly?
    By fattysmo in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2008, 11:37 AM
  3. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 10:46 PM
  4. How to properly use a .lib file?
    By Kurisu33 in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2006, 08:19 PM
  5. tic tac toe not working properly
    By h_howee in forum Game Programming
    Replies: 2
    Last Post: 01-01-2006, 01:59 AM