This would depend on how the pointer got to be a void*
to begin with. If it was cast to the void*
from the same type as be cast to (here MyClass*
), then yes, this cast is fine and works as expected;
From cppreference on static_cast
:
A prvalue of type pointer to void
(possibly cv-qualified) can be converted to pointer to any type. If the value of the original pointer satisfies the alignment requirement of the target type, then the resulting pointer value is unchanged, otherwise it is unspecified. Conversion of any pointer to pointer to void
and back to pointer to the original (or more cv-qualified) type preserves its original value.
Using static_cast
in this way is essentially saying to the compiler "I know it is this type - trust me", and the compiler obliges.
The dynamic_cast
can then be evaluated after this. It is typically used to cast to a more derived type. Here, you are casting to the same type - it is not doing anything particularly useful. If the type was a more derived type (e.g. MySpecialisedClass
), then it would be fine.
As it stands, the function can be simplified to:
MyClass* CastVoidPtr(void* pVoidPtr)
{
return static_cast<MyClass*>(pVoidPtr);
}
Or to simply use a naked static_cast<>
.
A side note; worth mentioning here for completeness is that reinterpret_cast
has similar functionality;
Any pointer to object of type T1
can be converted to pointer to object of another type cv T2
. This is exactly equivalent to static_cast<cv T2*>(static_cast<cv void*>(expression))
(which implies that if T2
's alignment requirement is not stricter than T1
's, the value of the pointer does not change and conversion of the resulting pointer back to its original type yields the original value). In any case, the resulting pointer may only be dereferenced safely if allowed by the type aliasing rules ...