semantics of init
semantics of init
- Subject: semantics of init
- From: Niko Matsakis <email@hidden>
- Date: Tue, 3 Jun 2003 20:02:41 -0400 (EDT)
Okay, so I'm quite new to Obj. C, but I'm wondering the following.
I read somewhere that the following code is dangerous, generally speaking:
id obj = [SomeClass alloc];
[obj init];
The manual I was reading said that 'init' may return a different value other
than self if it wants to, so to be safe, you should do:
id obj [[SomeClass alloc] init];
Okay, that seems fine. However, I'm wondering when you extend some class,
are you supposed to respect that rule in your own init functions?
For example, suppose I extend SomeClass with SomeOtherClass, should I
do something like this in the constructor?
- (void) init
{
id me = [super init];
me->myfields = some_data;
return me;
}
as opposed to
- (void) init
{
[super init];
myfields = some_data;
return self;
}
This seems stupid and annoying. I'm assuming that nobody in their
right mind would do anything but return 'self' from init, or if they
did so it would be well-documented and for some no doubt very worthy
cause, so I shouldn't worry about it. I'm wondering, though, if there is
some nice way to both be safe and not have to write annoying code like my
first example.
thanks!
niko
_______________________________________________
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.