[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: some objc compilers allow class variables?
From: |
gepr |
Subject: |
Re: some objc compilers allow class variables? |
Date: |
Fri, 24 Jan 2003 07:32:49 -0800 |
Paul Johnson writes:
> A user brought to my attention this claim in the objective-C faq
>
> http://www.faqs.org/faqs/computer-lang/Objective-C/faq/
>
> 15.1 What's the syntax for class variables ?
>
> List the class variables after the instance variables, and group them
> together in the same way as instance variables, as follows :
>
> @implementation MyClass : Object { id ivar1; int ivar2; } : { id cvar1; }
> @end
>
> This seems like a good feature to me, but appears only available with
> the "Portable Object Compiler". Has anybody heard any discussion of
> whether gcc might support this style someday?
I don't know about whether GCC will ever support that syntax; but,
you can create class variables by declaring them "static" in the
appropriate context.
The code below shows both how to do this *and* the flaw in relying
on scope to do this. In order to make this believable as a class
variable, you have to isolate the class variables to the methods
that deal with it. You isolate it by placement in a file (if you
put more than one implementation in a single file) or by presence
in an implementation file.
---
#import <simtools.h>
#import <objectbase/SwarmObject.h>
@interface Foo : SwarmObject
{
}
+(void) setLetter: (char) l;
-(char) getLetter;
@end
@implementation Foo
static char letter;
+(void)setLetter: (char) l {
letter = l;
}
-(char) getLetter {
return letter;
}
@end
@interface Bar : SwarmObject
{
}
-(char) getLetter;
@end
@implementation Bar
-(char) getLetter {
return letter;
}
@end
int main (int argc, const char **argv) {
id obj1, obj2, obj3 = nil;
initSwarm (argc, argv);
[Foo setLetter: 'c'];
obj1 = [Foo create: globalZone];
obj2 = [Foo create: globalZone];
fprintf(stdout,"[obj1 getLetter] = %c\n",[obj1 getLetter]);
fprintf(stdout,"[obj2 getLetter] = %c\n",[obj2 getLetter]);
obj3 = [Bar create: globalZone];
fprintf(stdout,"[obj3 getLetter] = %c\n",[obj3 getLetter]);
return 0;
}
---
--
glen e. p. ropella =><= Hail Eris!
H: 831.335.4950 http://www.ropella.net/~gepr
M: 831.247.7901 http://www.tempusdictum.com
==================================
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.