Re: Confused about superclass initializers



On Fri, Mar 27, 2009 at 10:53 AM, Roland King <rols@xxxxxxxx> wrote:
> I've thought myself into a hole in a subclass here ....
>
> If I have a class Foo with a subclass Bar, which adds say 2 new variables,
> in Bar's init method I do the usual
>
> Â Â Â Âself = [ super init ];
>
> after someone called me with
>
> Â Â Â ÂBar *myBar = [ [ Bar alloc ] init ];
>
> at the point I call that, 'self' is a Bar, it was alloc()ed to be long
> enough for a Bar. But it's quite possible that Foo's init will not init the
> object it's given, it will throw it away and return me a totally different
> Foo, [ super init ] doesn't have to return the same thing you sent it. But
> if that happens, it will be a Foo returned, it won't be a Bar, Foo's
> constructor has no idea that it's really meant to be making a Bar, it won't
> have enough memory allocated to be a Bar.
>
> Then bad things will happen when I try to set the Bar variables as I don't
> have enough memory for it.
>
> I must have missed something
> here.

This covers how all that works:

http://mikeash.com/?page=pyblog/the-how-and-why-of-cocoa-initializers.html

The short answer is that if Foo returns something different, it will
return something that's compatible with Bar, so everything still works
fine.

Mike
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@xxxxxxxxxxxxxxx)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.c...

om

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/maillists%40codeha.us

This email sent to maillists@xxxxxxxxx



Privacy