Re: Creating an instance of a subclass from a superclass initilization
Re: Creating an instance of a subclass from a superclass initilization
- Subject: Re: Creating an instance of a subclass from a superclass initilization
- From: Sherm Pendley <email@hidden>
- Date: Mon, 24 Apr 2006 17:05:18 -0400
On Apr 24, 2006, at 3:45 PM, Philip Dow wrote:
I have an object called EntryObject. There are two subclasses
EntryObjectFoo and EntryObjectBar. There are times when I am
allocating and initializing an EntryObject and it determines from
the objects I pass into this particular initializer that it should
be a subclass of itself. Instead of calling self = [super init],
could I call self = [EntryObjectFoo designatedInitializer] so that
the subclass receives a redirected init and returns the appropriate
class?
In general principle yes - if I understand you correctly, then what
you're doing is called a "class cluster" and is well supported in Cocoa.
In one particular though, I'd implement it differently. Even at this
low level, standard Cocoa memory management rules apply. Since the
"self" that's passed to your initializer was created with +alloc, it
needs to be released. Then you can alloc/init a new object to return.
Like this:
[self release];
return [[EntryObjectFoo alloc] designatedInit ...];
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden