sunlight07 sunlight07 - 1 year ago 108
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 Source

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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download