On Wed, May 27, 2009 at 9:48 AM, Freddie Tilley <fred...@cpp.nl> wrote:
> If I would want to allocate a class which allocates a single subclass
> instead, to
> hide the class implementation details
>
> would this be the correct way of doing it?
>
> The following code would be in the BaseClass
>
> + (id)allocWithZone:(NSZone*)aZone
> {
>        if ([[self class] isEqualTo:[BaseClass class]])
>        {
>                return [[SubClass alloc] init];

Close. You only want to do return [SubClass alloc] here. The caller
will be calling -init as usual after your method returns.

Alternatively, you can have a factory method which does both alloc and
init and returns that to the caller. Both work fine, it's pretty much
just a question of what you prefer. Overriding +allocWithZone: works
better if you have many different initializers for the caller to
choose from.

Mike
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please 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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to