sunlight07 sunlight07 - 1 month ago 6
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'

Answer

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: http://en.cppreference.com/w/cpp/language/reinterpret_cast

Comments