• 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
semantics of init
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Follow-Ups:
    • Re: semantics of init
      • From: Wade Tregaskis <email@hidden>
    • Re: semantics of init
      • From: Kyle Moffett <email@hidden>
    • Re: semantics of init
      • From: Sailor Quasar <email@hidden>
    • Re: semantics of init
      • From: Andrew Pinski <email@hidden>
  • Prev by Date: Editing BrowserCells only on Double Click --- how?
  • Next by Date: Re: semantics of init
  • Previous by thread: Window title color
  • Next by thread: Re: semantics of init
  • Index(es):
    • Date
    • Thread