Reputation: 235
I've recently seen a delegate method (that appears to work) and looks like this:
-(void) doSomethingWithThisPieceOfData:(void *)theData;
The delegate method then casts theData as:
-(void) doSomethingWithThisPieceOfData:(void *)theData { anObject *myObject; myObject = (anObject)theData; .... }
Why does this work and is it good coding practice? I would have used (id *)theData instead.
thanks.
Upvotes: 3
Views: 547
Reputation: 4339
It simply means that the size of the object to which the pointer refers is undeclared. The pointer will point to the beginning of the memory allocation and it is up to the receiver to cast or read the memory in an appropriate way.
If you specify the type, the pointer still points to the beginning of the memory allocation but if you increment its position, it will jump passed all the memory needed for that specific type and will point to the next block of memory.
With a void pointer, this doesn't happen. If you increment the position you can end up inside of the memory for your object rather than at the next object. You can get much better control over memory this way.
This previous SO answer might help you as well.
Upvotes: 0
Reputation: 39480
void * as a type indicates that any pointer can be passed, and that the code that recieves it will cast it to whatever type it considers appropriate.
Upvotes: 4