sunlight07 sunlight07 - 6 months ago 36
C++ Question

How to use reinterpret_cast in C++?

I know the reinterpret_cast in C++ can be used in this way:

float a = 0;
int b = *reinterpret_cast<int*>(&a);

But why cannot cast it directly?

float a = 0;
int b = reinterpret_cast<int>(a);

error: invalid cast from type 'float' to type 'int'


You can't reinterpret_cast in the case you give because reinterpret_cast takes only either a int to convert to a pointer, or the reverse, and follows additional similar rules.

There is a summary of these rules there: