• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Anyone here know basic C++?

Trevelyan

Diamond Member
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.

 
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).
 
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.
 
Back
Top