问题
What is the difference between :
double x = 10.3;
int y;
y = (int) x; // c-like cast notation
And :
double x = 10.3;
int y;
y = reinterpret_cast<int>(x)
回答1:
A C-style cast can be any of the following types of casts:
const_cast
static_cast
static_cast
followed by aconst_cast
reinterpret_cast
reinterpret_cast
followed by aconst_cast
the first one from that list that can be done is the what the C-style cast will perform (from C++03 5.4: "Explicit type conversion (cast notation)"
So for your example:
double x = 10.3;
int y;
y = (int) x;
the type of cast used would be a static_cast
.
And y = reinterpret_cast<int>(x);
won't compile.
来源:https://stackoverflow.com/questions/22999529/what-is-the-difference-between-a-reinterpret-cast-and-a-c-style-cast