Re: *?
Re: *?
- Subject: Re: *?
- From: "Dennis C. De Mars" <email@hidden>
- Date: Fri, 22 Jun 2001 19:27:07 -0700
on 6/22/01 11:06 AM, email@hidden at email@hidden wrote:
>
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?
I'm glad to hear you have Kernighan and Ritchie now. When you have time to
read through it, you'll find a lot more on the "*" contruction, it's pretty
central to C.
The thing is, pointer declaration and use have a fairly simple meaning in C,
but then in object-oriented languages like Objective C they add some extra
rules involving pointers to classes and subclasses, plus there is this type
called "id" which is more or less a pointer also but does not use a "*" in
its declaration.
So, I think it would be less confusing to learn the C version first and then
the Objective C additions. I know it is tempting to plunge directly into the
Cocoa stuff but I think it only took me about a week to read Kernighan and
Ritchie when I first encountered it and it is an interesting book in its own
right.
(I remember when I first encountered it, it was sometime in the 1970's. C
was not all that well know back then except probably by some computer
science grad students who were working with Unix. I was browsing at the UCLA
bookstore and saw this book called "The C Programming Language" and thought
that was a pretty weird name for a computer language. I skimmed through it
and decided it was pretty interesting, so I bought the book. I read through
it even though I had no immediate application for it; it was several years
before I even got my hands on my first C compiler.)
Anyway, to answer your question, all of those examples you cited are
examples of static typing. In each case "id" could have been substituted,
but then you wouldn't have static typing.
For instance, instead of:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
you could write
id pool = [[NSAutoreleasePool alloc] init];
So, in this case id was used instead of "NSAutoreleasePool *". In general,
id can be used in any declaration in place of "AnyClass *". The drawback is
that if you use a method that NSAutoreleasePool doesn't respond to, the
compiler won't object, so if you wrote:
[pool DefinitelyNotAnAutoreleasePoolSelector];
then the compiler would not give you a warning, but you'd get a runtime
error. With the original declaration, the compiler would warn you.
>
>
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, the space is optional. You could write:
NSAutoreleasePool *pool
or
NSAutoreleasePool* pool
or
NSAutoreleasePool * pool
or
NSAutoreleasePool*pool
The choice of whether to put in spaces and where is purely a matter of
personal style. Of the choices above, almost everyone would pick one of the
first two. Everyone has some logical rationalization for their own style,
based on their own idea of readability, but the compiler doesn't care.
- Dennis D.
References: | |
| >*? (From: email@hidden) |