Re: abstract class
Re: abstract class
- Subject: Re: abstract class
- From: Bill Bumgarner <email@hidden>
- Date: Thu, 24 May 2007 16:49:49 -0700
On May 24, 2007, at 3:35 PM, Serge Cohen wrote:
To come back to the original post... I was also wondering how the
'class-cluster' were implemented...
Clearly this does not require that the top class is abstract. Still
I have troubles to understand how the allocation/init works for
these cluster classes.
Can anyone provide an example ?
Sure. First, there is no such thing as an abstract class in Objective-
C beyond the concept of abstract; that is, there is no syntactic
indicator of abstractedness. It is all runtime behavior.
In a class cluster, the top class -- say NSArray or NSMutableArray --
is typically abstract. That is, you'll never actually work with an
instance of that class at runtime, always a subclass. This is not a
requirement, but it makes dealing with the code easier.
Typically, you'd do something like:
@implementation MyPublicClassClusterTopClass
- initWithFoo: (Foo *) aFoo
{
[self release];
self = [[MyPrivateClassClusterConcreteSubclass alloc] initWithFoo:
aFoo];
return self;
}
@end
That is, your abstract initializers would not actually return the same
instance that was originally being initialized.
You can see this in action in class cluster:
NSArray *x = [NSArray alloc];
NSLog(@"x: 0x%x", x);
x = [x initWithObjects: [NSObject new], [NSObject new], nil];
NSLog(@"x: 0x%x", x);
x: 0x104020
x: 0x105900
Note that the value of 'x' changed. That is because -
initWithObjects:, as is typical of class cluster initialization
methods, went off and allocated an instance of whatever private
subclass of NSArray is optimized to handling -initWithObjects: style
arrays.
b.bum
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden