The
(double)a
is a C-style cast. An instrument so blunt that the C++ introduced a new cast syntax:
static_cast<double>(a)
The static_cast is not the only one; there are others too. All more specific than the C-style bludgeon.
Lets assume that there are two division operators:
1 2
|
int operator/ (int, int); // #1
double operator/ (double, double); // #2
|
Both a and b are
int
, so it is clear that the compiler uses #1 for
a / b
.
If you do cast both a and b to
double
, then the use of #2 is obvious.
What if only one side is double, as in
static_cast<double>(a) / b
?
The compiler has two options:
1.
Implicitly convert b to double and then call #2
2.
Implicitly convert a to int and then call #1
Option 1 wins. All these call the #2:
1 2 3
|
static_cast<double>(a) / b
a / static_cast<double>(b)
static_cast<double>(a) / static_cast<double>(b)
|
SakurasouBusters'
(double)(1.0 * b)
is "tårta på tårta". The multiplication has two operands, a double (1.0) and int (b). Therefore, b converts implicitly to double, the multiplication operates on doubles and the result is a double. An explicit C-style cast to double does nothing to a double value.