• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Objective C language question: Making init like a contrustor
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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 12:16:33 +0200

On Sunday, May 26, 2002, at 11:52 , Ondra Cada wrote:

If I understand correctly (!), he fears this pattern:

Sorry, forgot the inheritance (@interface B:A). I hope it was obvious enough so that the mistake did not blur the reasoning. Anyway, here's the fixed version:

@implementation A
-init {
[super init];
[self foo];
return self;
}
-(void)foo { whatever }
@end

@interface B:A { int mustBeFourForWhateverReason; } @end
@implementation B
-init {
[super init];
mustBeFourForWhateverReason=4;
return self;
}
-(void)foo {
// crashes if mustBeFourForWhateverReason!=4
}
@end

He does not realize though 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).
---
Ondra Hada
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.
References: 
 >Re: Objective C language question: Making init like a contrustor (From: Ondra Cada <email@hidden>)

  • Prev by Date: Re: Objective C language question: Making init like a contrustor
  • Next by Date: Re: Objective C language question: Making init like a constructor
  • Previous by thread: Re: Objective C language question: Making init like a contrustor
  • Next by thread: Re: Objective C language question: Making init like a contrustor
  • Index(es):
    • Date
    • Thread