[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: question on id vs obj *
From: |
Paul E Johnson |
Subject: |
Re: question on id vs obj * |
Date: |
Wed, 30 Aug 2000 08:31:28 -0500 |
Ken Gosier wrote:
>
> Quick question on declaring an object as id vs obj * (say List * to be
> concrete.)
>
> I'm working through the user guide now, and Sec 3.5 has a discussion of
> the different types of declarations. Here it says that List * is no
> longer allowed, since there really no List class, only a protocol, which
> you may declare an id to follow.
>
> However, I see in all the tutorials, that methods called by the
> 'factory' object use the List * type syntax. (Example:
> tutorial/simpleObserverBug/ModelSwarm.m, createBegin, which declares obj
> as ModelSwarm *)
When you write a class yourself, you have to choice of describing it as
id obj;
or
ModelSwarm * obj;
The first one say obj is an object, without saying what type. THe
second says specifically what it is, and it has the advantage that, at
compile time, you will get error messages if you tell obj to do
something it can't do. Hence, when you can do it, an explicit usage is
better, and so the second one is what I would use.
If you declared a protocol for your class ModelSwarm, then you could do
id <ModelSwarm> obj;
but most of the time people don't bother.
Concerning Swarm things, many are not classes, and so, if you want
compile time error checking, you have to use the protocol in the
declaration.
I believe you cannot use id if you want to access instance variables
like this
obj->aVariable = 80;
objects have to be statically typed for that. Try one with the id and
the compiler complains about it.
--
Paul E. Johnson email: address@hidden
Dept. of Political Science http://lark.cc.ukans.edu/~pauljohn
University of Kansas Office: (785) 864-9086
Lawrence, Kansas 66045 FAX: (785) 864-5700
==================================
Swarm-Support is for discussion of the technical details of the day
to day usage of Swarm. For list administration needs (esp.
[un]subscribing), please send a message to <address@hidden>
with "help" in the body of the message.