Re: class variables and class methods
Re: class variables and class methods
- Subject: Re: class variables and class methods
- From: Michael Latta <email@hidden>
- Date: Wed, 7 Jan 2004 20:08:11 -0800
The short answer is: "Smalltalk had that 30 years ago, but C won the
war."
There is a longer answer if you control all the classes involved: "Do
not use class methods." Use factory or singleton patterns. There is
only one way to obtain class Asub when the code thinks it is Asuper.
That is by asking an instance for it's class, or obtaining the class
object from some data structure. In the case of asking an instance,
just make it an instance method. In the case of accessing the class
object from some data structure, access a factory instance instead. It
can act as a proxy for the class, but override the implementation of
any methods it likes. Since you are invoking the method on what the
run-time thinks is an instance rather than a static reference, see part
1 of this answer.
Michael
If you need inheritance properties in class scope, do it using a class
accessor on the static variable, ie:
in .h file:
+ (int)myClassVariable;
in .m file:
static int myClassVariable = 0;
+ (int)myClassVariable {return myClassVariable;}
But this is the whole problem. If I then subclass this class and wish
myClassVariable to hold something different in the subclass but still
work with the inherited accessor / other methods, it won't. The
superclass's meClassVariable is used instead :(
Robert
---
GnuPG public key:
http://www.Far-Blue.co.uk
[demime 0.98b removed an attachment of type application/pgp-signature
which had a name of PGP.sig]
_______________________________________________
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.
_______________________________________________
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.