Re: newbie pointer dilemma...
Re: newbie pointer dilemma...
- Subject: Re: newbie pointer dilemma...
- From: Matt Neuburg <email@hidden>
- Date: Sat, 31 Aug 2002 04:06:26 -0700
On Fri, 30 Aug 2002 22:54:05 -0400, Jonas Roel <email@hidden> said:
>
>
Can someone cogently differentiate between these two types of pointers:
>
>
NSRect *xyz;
>
&
>
NSRect xyz;
>
>
in others words when and why should one use/omit the * ?
One should use the * if one wants a pointer to an NSRect, and omit it if
one wants an NSRect. :-) However, that isn't the answer you wanted, because
you asked the wrong question. The question you wanted to ask is: why does
one typically say
NSString* s;
but
NSRect r;
One way to look at the matter is that when you declare variable storage
like this you're getting ready to receive an actual value to be stored
there. All the calls that return an NSString (such as +NSString
stringWithFormat:) return a pointer to an NSString, so it is right to
prepare for this by setting up an NSString*. But just about all the calls
that return an NSRect (such as NSMakeRect()) return an NSRect plain and
simple, so it is right to prepare for this by setting up an NSRect.
Another way to look at it is that an NSString is an object (NSString is the
name of a class) but an NSRect is not (NSRect is the name of a struct).
This is not the same thing as what I said in the previous paragraph but
there is a close relationship to it. For the same reason, by the way, you
won't be sending any messages to an NSRect; the compiler will merely waggle
its finger at you if you say
[s fakeMethod];
but it will slap you upside the face if you say
[r fakeMethod];
m.
--
matt neuburg, phd = email@hidden,
http://www.tidbits.com/matt
pantes anthropoi tou eidenai oregontai phusei
Subscribe to TidBITS! It's free and smart.
http://www.tidbits.com/
_______________________________________________
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.