Page 50


#1

id - Page 50

Hi all, isn’t this book great? :smiley:

I was wondering if anyone could shed some more light on this extract from the book:

[quote]Take another look at the initializer’s declaration. Its return type is id, which is defined as “a pointer
to any object.” (This is a lot like void * in C and is pronounced “eye-dee.”) init methods are always
declared to return id.[/quote] - I get this

[quote]Why not make the return type Possession * – a pointer to a Possession? After all, that is the type
of object that is returned from this method. A problem will arise, however, if Possession is ever
subclassed.[/quote] - I get

[quote]The subclass would inherit all of the methods from Possession, including this initializer
and its return type. An instance of the subclass could then be sent this initializer message, but what
would be returned? Not a Possession, but an instance of the subclass.[/quote] - I am not clear why a Possession wouldn’t be returned if the class was subclassed, what changes in the subclass if a class is subclassed?

Thanks guys!


#2

I’m not clear on how this happens either, but Apple’s docs say the same thing:

Perhaps the compiler is checking explicitly for assignment to self and forces the type to be that of its own class; I don’t know.


#3

gc3182 thanks for your reply, I will take Apple’s word for it…it must be one of these little mysteries!


#4

Hi,

If you are interested to see what’s happening “under the covers” this article is very good.


cocoasamurai.blogspot.com/2010/0 … ntime.html

I wouldn’t recommend reading it when you’re starting out but worth a read once you’ve finished the book.

Gareth


#5

Thanks for the link, looks like an intesresting read, I’ll take a look once I have finished the book and have more understanding of the language.