Am I using casts properly?

This is a discussion on Am I using casts properly? within the C++ Programming forums, part of the General Programming Boards category; There are some pretty big gaps in my programming knowledge, one of them being casts. I was writing code like ...

  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
    22,166
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    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
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    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...
    - Leeor

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, 12:37 PM
  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, 09: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

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