Skip to main content
gamedev.net
🔒 Locked

(C++) Casting

Started by Appmonster 4 replies 850+ views
Original Post
Appmonster
Appmonster
Stumbled on this thing called casting, had a look around and found a decent website that shows you the many ways to do it, but I don't know what the standard way of doing it is and now i'm confused (lol), can anybody clear casting up for me?
dave
dave
In C++ you should be casting using one of the following, as appropriate:

static_cast<>()
dynamic_cast<>()
reinterpret_cast<>()

Googling for each of those will yield examples of usage.

Hope that helps,
Appmonster
Appmonster
thank you, will do! Just figured out this cast lol:

int  a = 97;char b = 'z';     b = (char) a; // Turns b into a (from z)
dave
dave
Quote:
Original post by Appmonster
thank you, will do! Just figured out this cast lol:

*** Source Snippet Removed ***


With that cast it is important to note that you are truncating the data stored in the integer since that is most likely 32 bits and the char is most likely 8 bits. So the first 8 bits of the integer are preserved, the rest is lost.
DevFred
DevFred
Quote:
Original post by Dave
In C++ you should be casting using one of the following, as appropriate:

static_cast<>()
dynamic_cast<>()
reinterpret_cast<>()

You forgot const_cast.
dave
dave
Quote:
Original post by DevFred
Quote:
Original post by Dave
In C++ you should be casting using one of the following, as appropriate:

static_cast<>()
dynamic_cast<>()
reinterpret_cast<>()

You forgot const_cast.


Shows how little i use it. Which is a good thing :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.