Re: id vs cast
Re: id vs cast
- Subject: Re: id vs cast
- From: Fritz Anderson <email@hidden>
- Date: Fri, 6 Dec 2002 23:21:06 -0600
On Friday, December 6, 2002, at 09:57 PM, Edward Fink wrote:
I am dealing with a function that returns a void pointer.
I assume you don't have ownership of that function? Because if you
know, as you seem to, that it returns a pointer of a very specific
type, it seems perverse to declare it as a function returning (void *).
I take this pointer and cast it to "TheClass" like this:
TheClass *myObject;
myObject = (TheClass *)thePointer;
I am curious if there is any benefit to doing:
id *myObject = thePointer;
instead of casting the pointer as I have above.
Okay, first: You do not mean (id *). id, without an asterisk, means
"pointer to an Objective-C object." id * is a pointer to such a
pointer. Sending a message to an (id *) won't work.
So:
void * thePointer; // known to point to a TheClass
TheClass * myObject;
id alsoMyObject;
myObject = (TheClass *) thePointer; // A
alsoMyObject = (id) thePointer; // B
A and B are roughly-equivalent statements. If you have methods or
functions that expect parameters of type (TheClass *), the compiler
will complain if, instead, you pass an id variable. (But why keep the
type-checking straight yourself, when you have a computer to do it for
you?)
I have used both ways with no problems. Is there a performance benefit
either way?
Both A and B will compile to a single load of a register. There's no
run-time difference.
-- F
--
Fritz Anderson - Consulting Programmer - Chicago, IL
Mail: <email@hidden>
Risumi: <
http://resume.manoverboard.org>
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: | |
| >id vs cast (From: Edward Fink <email@hidden>) |