Anyone here know basic C++?

Trevelyan

Diamond Member
Dec 10, 2000
4,077
0
71
Say I have this:

cout << 10.2 + static_cast <int> (18.2) / 4;

So the output will be 14.2, but what will the data type be? float or double?

Learning C++ is a lot of fun, but I'm a little confused on this one... since the numbers aren't variables that have a predefined type, I don't know what the data type is for the output.

Thanks for any help you can provide.

 

imported_plasmasnake

Junior Member
Aug 28, 2005
17
0
0
Double, at least with the compiler I'm using. Wrap sizeof() around the expression, you'll see you get 8 bytes as your answer (sizeof(float) is 4).

Curious, though; you're the first person I've *ever* seen use a C++ style cast instead of the old C casts (just placing the desired cast in parentheses next to the expression).
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Well, static cast is sort of the equivalent of the C-style explicit casts in the C++ standard. Would be equivalenet to

cout << 10.2 + (int)(18.2)/4;

So the result of the static cast is of type int, value 18, divided by four yields type int, value 4, added to the double typed literal yields a double.