71

Possible Duplicate:
When should static_cast, dynamic_cast and reinterpret_cast be used?

I'm using c function in c++, where a structure passed as a void type argument in c is directly stored that same structure type.

eg in C.

void getdata(void *data){
    Testitem *ti=data;//Testitem is of struct type.
}

to do the same in c++ i use static_cast:

void foo::getdata(void *data){
    Testitem *ti = static_cast<Testitem*>(data);
}

and when i use reinterpret_cast it does the same job, casting the struct

when i use Testitem *it=(Testitem *)data;

this does the same thing too. But how is the structure gets affected by using the three of them.

3
  • that thread says about when to use it. I'm asking about whats differs to that structure when using these three thing, and i forgot about dynamic_cast too.Simply put i wanna know how the structure is affected. Jul 28, 2011 at 8:00
  • The structure isn't affected (unless casting between numeric types).
    – Jim Balter
    Apr 2, 2017 at 3:50
  • Casting from void* using static_cast and using reinterpret_cast gives the same result: stackoverflow.com/a/68137312/5447906.
    – anton_rh
    Jun 26, 2021 at 6:13

1 Answer 1

145

A static_cast is a cast from one type to another that (intuitively) is a cast that could under some circumstance succeed and be meaningful in the absence of a dangerous cast. For example, you can static_cast a void* to an int*, since the void* might actually point at an int*, or an int to a char, since such a conversion is meaningful. However, you cannot static_cast an int* to a double*, since this conversion only makes sense if the int* has somehow been mangled to point at a double*.

A reinterpret_cast is a cast that represents an unsafe conversion that might reinterpret the bits of one value as the bits of another value. For example, casting an int* to a double* is legal with a reinterpret_cast, though the result is unspecified. Similarly, casting an int to a void* is perfectly legal with reinterpret_cast, though it's unsafe.

Neither static_cast nor reinterpret_cast can remove const from something. You cannot cast a const int* to an int* using either of these casts. For this, you would use a const_cast.

A C-style cast of the form (T) is defined as trying to do a static_cast if possible, falling back on a reinterpret_cast if that doesn't work. It also will apply a const_cast if it absolutely must.

In general, you should always prefer static_cast for casting that should be safe. If you accidentally try doing a cast that isn't well-defined, then the compiler will report an error. Only use reinterpret_cast if what you're doing really is changing the interpretation of some bits in the machine, and only use a C-style cast if you're willing to risk doing a reinterpret_cast. In your case, you should use the static_cast, since the downcast from the void* is well-defined in some circumstances.

3
  • 1
    It's also possible double pi = 3.1415; int ipi = static_cast<int>(pi); but in case of link you must use reinterpret_cast double* pi = 3.1415; int* ipi = reinterpret_cast<int*>(pi); Oct 4, 2019 at 11:49
  • Still very helpful answer! I'm writing custom allocators, and using dynamic_cast all the time to convert between uintptr_t and void*.
    – alexpanter
    Mar 23, 2021 at 10:31
  • Neither static_cast nor reinterpret_cast can remove const from something. Directly usage, true, but passively usage, not true. Look at this: const int n = 9; const int* p = &n; int* i = reinterpret_cast<int*>(reinterpret_cast<unsigned>(p)); This is for 32-bit platform and on 64-bit, change the unsigned to unsigned long long or use uintptr_t for both. Aug 6 at 12:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.