Re: Subclass
Re: Subclass
- Subject: Re: Subclass
- From: Prachi Gauriar <email@hidden>
- Date: Sat, 14 Jun 2003 16:11:15 -0500
On Saturday, June 14, 2003, at 02:39 PM, Sherm Pendley wrote:
On Saturday, June 14, 2003, at 04:22 AM, Sailor Quasar wrote:
3. The ability to declare static instance vars (instance vars that
are part of the class object rather than of any instance of that
class). If I've missed this functionality somewhere, please tell me
where it is. I generally use globals to emulate it, which I
personally feel is bad style.
I take advantage of a "C-ism" to more or less simulate class
variables. If you declare your globals as "static", they're only
visible from within the same file.
There are obviously cases where this approach breaks - more than one
class declared in a file, or a class that's in several categories that
are spread across multiple files, for instance. But for the simplest
and most common case, where a file contains a single class, and that
class is entirely defined in that file, it's adequate.
The way around this problem is to provide a class method to access the
variable like so:
static MyClass *aClassVariable = nil;
+ (MyClass *)aClassVariable
{
if (aClassVariable == nil) {
aClassVariable = [[MyClass alloc] init];
}
return [[aClassVariable retain] autorelease];
}
and write a setter as well. This is essentially a class variable, and
gets around the fact that you can't access it out of that file. Those
of you that are optimization crazy might find all this message passing
this too much to bear, but you probably don't use accessors (or Obj-C
for that matter) because of the messaging overhead.
_______________________________________________
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: | |
| >Re: Subclass (From: Sherm Pendley <email@hidden>) |