static_cast

Nov 17, 2010 at 4:01am
I'm sorry, but I've looked for this everywhere. In my book they start using this and don't say what it is. I've looked on the internet, but it still is unclear to me exactly what static_cast does. Can anyone help?

Sorry for the dumb question,
Jeff
Nov 17, 2010 at 4:11am
closed account (1yvXoG1T)
A static cast just acts as a temporary conversion from one type to another.
For example:
1
2
3
4
5
6
7
8
9
int main() {
    int a = 97; // The decimal equivalent of 'a'
    cout << static_cast<char>(a) << endl; // "Cast" the 97 as it's char counterpart

    return 0;
}

Output:
a
Nov 17, 2010 at 4:51am
Okay, that makes sense.
Thanks a lot.
Nov 17, 2010 at 6:03am
closed account (S6k9GNh0)
http://msdn.microsoft.com/en-us/library/c36yw7x9%28VS.80%29.aspx

This is for the VC++ compiler but their static_cast is standard compliant as far as I can tell (meaning this should work on any compiler you use). Please note that it's not just a temporary conversion from one type to another.
Topic archived. No new replies allowed.