In cpp
EBD_HEADER* pHeader;
pHeader = reinterpret_cast <EBD_HEADER*>(reinterpret_cast<void*>(const_cast <char*> (GetHeader())));
In Objective C
EBD_HEADER* pHeader;
pHeader = (EBD_HEADER*)((void*)((char*)([self GetHeader])));
Why is these so many casts needed. Is it wrong to convert it into (EBD_HEADER*)[self GetHeader];
You don’t need that many casts in C/Objective-C, you should just use one cast for simplicity:
With C++-style casts, you need two casts because one casts away the
const-ness and the other reinterprets the pointer to one type to a pointer to another type; the intermediate cast tovoid*is redundant and should be removed.However, in this case, you should try to refactor the code so that you don’t need any casts at all. Why does
[self GetHeader]return aconst char*instead of anEBD_HEADER*? Is the object returned actually constant? If so, you should declarepHeaderasconst EBD_HEADER*, and you should not cast away theconst-ness; if not, then[self GetHeader]should not return aconsttype.