Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Casting signed int to unsigned int? (http://www.programmingforums.org/showthread.php?t=14571)

null_ptr0 Nov 23rd, 2007 8:35 PM

Casting signed int to unsigned int?
 
I know I could use simple C casts (unsigned int uint_fld = (unsigned int) ivar;) but I'm not sure if I should use some of the handy casting functions (unsigned int uint_fld = dynamic_cast<unsigned int>(ivar);). Which should I chose?


sidequestion: I have countlessly just casting something unsigned through
:

  1. unsigned char uchar_var = (unsigned) char_var;

, but is this appropriate for c++ programming?

Ancient Dragon Nov 23rd, 2007 9:27 PM

Re: Casting signed int to unsigned int?
 
Most c++ pureists would say that it is NOT appropriate for c++. IMO (for what its worth) I think it depends -- in your example it would be ok, but in more complex situations it would not. If you do use the c-style castings you must make sure you absolutely KNOW what you are doing because the compiler will simply assume you are correct -- no error checking. The compiler will produce warnings or error with c++ casts.


All times are GMT -5. The time now is 3:49 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC