Syntax:
int (minutes)
is called "function cast" or "functional-style cast", because it looks like a function call. It does the same as C-style cast (int)minutes
— truncates double
to int
. The difference between this and static_cast
is only in syntax: the latter is more verbose, so easier to search for in code.
static_cast
is different from C-style cast in type-safety — it fails to do suspicious conversions (e.g. integer to pointer). Other than that, it's identical.
Semantics:
hours = static_cast<double>(minutes)/ 60
This does floating-point division, generating a floating-point result. If hours
has floating-point type (e.g. double
), it will retain fractional part (e.g. 10 minutes is 0.16666666666666666 hours).
If hours
has integer type, the compiler will generate type conversion from double
to int
(and probably a warning) and truncate the number of hours. This is bad code, because it hides some of your calculation logic behind obscure language rules. If you want to convert from double
to int
, do it explicitly using static_cast
or C-style cast.
double minutes;
hours = int (minutes)/ 60
This truncates minutes to integer and calculates an integer number of hours from that. A bit surprising that minutes
can hold non-integer values but hours
cannot, so you might want to explain your calculation logic in a comment.