Re: *?
Re: *?
- Subject: Re: *?
- From: jgo <email@hidden>
- Date: Fri, 22 Jun 2001 17:56:43 -0700
>
I keep running into things like:
>
>
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
>
>
and:
>
>
- (NSString *)airports;
>
- (void)setAirports:(NSString *)str;
>
>
Now, I understand the use of the * in:
>
>
NSMutableArray *myArray;
>
>
which says that myArray is of type "NSMutableArray" instead of generic
>
type "id". But there is no explanation that I can find as to what the *
>
does or means in the first two examples. My copy of Kernighan and
>
Ritchie just came and I looked there, but all I could find was a usage
>
somewhat similar on page 94 that uses the * to indicate that an argument
>
is a pointer to a type. So, all I can guess is that in the example:
>
>
- (NSString *)airports;
>
>
the * is shorthand for statically typing "airport" to NSString. Is that
>
right? If so, then in the second line:
>
>
- (void)setAirports:(NSString *)str;
>
>
"str" is statically typed to NSString also?
>
>
As for the first example:
>
>
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
>
>
is this the same as:
>
>
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];?
>
>
Is the space between the * and "pool" optional? If so is there some
>
reason to use the extra space in this construction?
Yes. In general a declaration with a * in it means the variable is a
pointer to something of the type designated (as you found in K&R), so
char * fred;
means that fred is a pointer to char, and the space for char is
usually dynamically allocated using fred = new char or
fred = new char[sizeOfCharString]
(or in OS 9- for unsigned char * fred it would be
fred = NewPtr(sizeOfCharString)) or the equivalent.
In Objective-C all "static" declarations of objects are
NSQuark * thisobject;
because, in practice, instances of objects are blocks of memory
pointed to by the variable.
If you look in the docs or _LC_ pg 29, for example, you'll see
id [is] an object (a pointer to [an object's] data structure)
and then on page 76 you see that "Generic outlets are declared as:
IBOutlet id variableName;" while their modified "static" equivalent
is "IBOutlet NSTextField * textField;"
There are at least 3 schools on spacing the * in such declarations:
type* var;
type *var;
type * var;
John G. Otto Nisus Software, Engineering
www.infoclick.com www.mathhelp.com www.nisus.com software4usa.com
EasyAlarms PowerSleuth NisusEMail NisusWriter MailKeeper QUED/M
Will program Macs for food.
References: | |
| >*? (From: email@hidden) |