C++

Asked • 09/06/19

When to use reinterpret_cast?

I am little confused with the applicability of `reinterpret_cast` vs `static_cast`. From what I have read the general rules are to use static cast when the types can be interpreted at compile time hence the word `static`. This is the cast the C++ compiler uses internally for implicit casts also. `reinterpret_cast`s are applicable in two scenarios, convert integer types to pointer types and vice versa or to convert one pointer type to another. The general idea I get is this is unportable and should be avoided. Where I am a little confused is one usage which I need, I am calling C++ from C and the C code needs to hold on to the C++ object so basically it holds a `void*`. What cast should be used to convert between the `void *` and the Class type? I have seen usage of both `static_cast` and `reinterpret_cast`? Though from what I have been reading it appears `static` is better as the cast can happen at compile time? Though it says to use `reinterpret_cast` to convert from one pointer type to another?

1 Expert Answer

By:

Kyle A. answered • 05/05/20

Tutor
New to Wyzant

Senior Software Engineer Specializing in Systems Programming

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.