What is the difference between static_cast<double>(votesTotal) , and the way you did it with just (double)votesTotal??? |
In this case, it would do the same thing as
(double)votesTotal.
However, in general, there's a couple factors, some technical, some not.
•
static_cast<type>(var) is the modern C++ way of doing it. [/b]
•
(type)var is the old C way of doing it.
The advantages of C++'s static_cast are that it is more type-safe, and can improve readability by making meaning unambiguous. static_casts only allow type-to-type conversion, and not pointer-to-type conversion or vice-versa. C-style casts don't guarantee this, so it's easier to introduce a bug. Especially in C++, parentheses are a commonly overloaded operator. Having the "cast" keyword immediately makes it clear to the programmer that the cast is
intentional. C++ generally tries to be more type-safe than C, so in practice you should need less casts than the old C days. Lots of casting points to some flaw in design. A cast every now and then to make division work is fine, though.
It also exists to explicitly differentiate between casts like
reinterpret_cast and
dynamic_cast, which are also useful, but offer different behavior than static_cast.