Re: Objective C language question: Making init like a contrustor
Re: Objective C language question: Making init like a contrustor
- Subject: Re: Objective C language question: Making init like a contrustor
- From: Ondra Cada <email@hidden>
- Date: Sun, 26 May 2002 11:52:19 +0200
On Sunday, May 26, 2002, at 07:10 , Jeff LaMarche wrote:
The basic question: Is it possible to design a runtime so that init's can
act like constructors?
My first thought as I read your question is, "Why?" I have lost touch with
C++ and the myriad directions it has sprawled into, but I simply don't see
why you would ever have any reason to implement this type of behavior.
If I understand correctly (!), he fears this pattern:
@implementation A
-init {
[super init];
[self foo];
return self;
}
-(void)foo { whatever }
@end
@interface B { int mustBeFourForWhateverReason; } @end
@implementation B
-init {
[super init];
mustBeFourForWhateverReason=4;
return self;
}
-(void)foo {
// crashes if mustBeFourForWhateverReason!=4
}
@end
Of course, he does not realize that exactly the same pattern might bring
problems for *any* inheritance (in his terminology, virtual one). It is
just a thing one must take care of (by documenting how init behaves when
writing a class, and taking this information seriously when subclassing it)
, and it is quite a negligible price for
(a) not messing up the object model by complicated exceptions and special
behaviours;
(b) the possibility of designated initializers, which automatically ensure
the complete potentially very rich "constructor" palette is inheritable
without any extra effort.
Of course, if I overlooked anything, someone please correct me (but so far
as my 10+ year ObjC and 5+ year C++ experience go, I would be surprised).
Objective-C
is weakly typed and late bound, so I don't understand what you would
achieve
by doing this.
Breaking that ;)
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
2K Development: email@hidden
http://www.2kdevelopment.cz
private email@hidden
http://www.ocs.cz/oc
_______________________________________________
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.