How to use static_cast conversion in C++
This article mainly introduces how to use static_cast conversion in C++, the article is very detailed, has a certain reference value, interested friends must read it!
Static_cast is a cast operator. Cast, also known as explicit conversion, C++ cast operators static_cast, dynamic_cast, const_cast, reinterpert_cast four. This section describes the static_cast operator.
Conversion of static_cast
Any type conversion implicitly performed by the compiler can be done by static_cast, such as conversion between int and float, double and char, enum and int, and so on.
Doublea=1.999
Intb=static_cast (a); / / equivalent to astatb
When compilers implicitly perform type conversions, most compilers give a warning:
E:\ vs2010projects\ static_cast\ static_cast\ static_cast.cpp (11): warningC4244: "initialization": convert from "double" to "int". Data may be lost.
Using static_cast, you can explicitly tell the compiler that this loss of precision conversion is done knowingly, and it can also let other programmers reading the program know the purpose of your conversion rather than negligence.
Convert the type with high precision to the type with low precision, and static_cast uses bit truncation to deal with it.
Using static_cast, you can retrieve the value stored in the void* pointer.
Doublea=1.999
Void*vptr=&a
Double*dptr=static_cast (vptr)
Cout