• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: id vs cast
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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>)

  • Prev by Date: Re: Is Objective-C loaded?
  • Next by Date: Re: id vs cast
  • Previous by thread: id vs cast
  • Next by thread: Re: id vs cast
  • Index(es):
    • Date
    • Thread