Reputation: 755
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];
Upvotes: 1
Views: 483
Reputation: 400642
You don't need that many casts in C/Objective-C, you should just use one cast for simplicity:
pHeader = (EBD_HEADER*)[self GetHeader];
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 to void*
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 a const char*
instead of an EBD_HEADER*
? Is the object returned actually constant? If so, you should declare pHeader
as const EBD_HEADER*
, and you should not cast away the const
-ness; if not, then [self GetHeader]
should not return a const
type.
Upvotes: 4