Jump to content
  • Advertisement
Sign in to follow this  
thedustbustr

how to c++ cast from int so ushort

This topic is 4032 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

the int will always be positive and within the range of possible values for a short. external library has poor design. int x = getWidth(); unsigned short xx = x; //how to cast this in c++

Share this post


Link to post
Share on other sites
Advertisement
unsigned short xx = static_cast<unsigned short>(x);


Quote:
Original post by ToohrVyk
EDIT: mine's formatted better [wink]


Not any more [razz].

Share this post


Link to post
Share on other sites
The reinterpret_cast is bad for a lot of reasons. Number one of which is that it won't work properly in a number of situations. For example, using it on a big-endian machine will cause it to reinterpret the wrong set of bytes. There's no reason to get cute when doing a simple cast.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
The reinterpret_cast is bad for a lot of reasons. Number one of which is that it won't work properly in a number of situations. For example, using it on a big-endian machine will cause it to reinterpret the wrong set of bytes. There's no reason to get cute when doing a simple cast.


I completely agree. I was indeed trying to be cute, and in no way meant it to be taken seriously.

Share this post


Link to post
Share on other sites
Why are you copying it to an unsigned short? What do you gain? The fact that it is "always" positive and that it fits in a short are not really very good reasons.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!