Skip navigation.
 
mlRe: Are polymorphic objects possible?
FROM : Chris Hanson
DATE : Sat Nov 02 21:34:18 2002

At 7:49 PM +1100 11/2/02, Wade Tregaskis wrote:
>>Just change the "isa" and you're done. The only real use I know of
>>is "thunks" for lazy evaluation.
>>
>>self->isa=[WTFoo class];

>
>That is indeed very easy.  What happens, however, when a message is
>sent to my WTMorphy object, a message which only say WTFoo accepts?
>Will that message only be passed on if WTMorphy is pretending to be
>WTFoo, and 'lost' if it's pretending to be WTBar?  Or will the
>message always be passed on so long as the receiver for it actually
>exists, irrelevant of what isa thinks?


No.  Messages are always sent to objects.  If your object doesn't
handle a message it receives, the runtime will send your object
-methodSignatureForSelector: followed by -forwardInvocation:.
NSObject's implementation of -forwardInvocation: just sends your
object -

>This prompts the question as to whether two identical method
>declarations on two different objects translate to the same
>"address", if I can continue using the message metaphor.  i.e. will
>a message intended for "myUniqueMethodName" on object WTBar still go
>to "myUniqueMethodName" on object WTMorphy?


I think I know what your problem is.  You're still thinking in C++ --
that is, you're assuming there's some sort of "tight" binding between
messages and objects.  There isn't.

When you write [myObject myUniqueMethodName] the compiler translates
this into something like

  objc_msgSend(myObject, @selector(myUniqueMethodName));

There's no static binding at all between myObject and
@selector(myUniqueMethodName); the instance method in myObject
corresponding to @selector(myUniqueMethodName) is looked up at
runtime and invoked.  (Or, if one isn't found in myObject or any of
its superclasses, the runtime does the -methodSignatureForSelector:
and -forwardInvocation: dance.)

So if myObject is an instance of WTMorphy, the runtime will try to
look up the instance method for -[WTMorphy myUniqueMethodName].  If
myObject is an instance of WTFoo, the runtime will try to look up the
instance method for -[WTFoo myUniqueMethodName].  And so on.

Is this clearer?

  -- Chris

--
Chris Hanson                      |  Email: <email_removed>
bDistributed.com, Inc.            |  Phone: +1-847-372-3955
Making Business Distributed      |  Fax:  +1-847-589-3738
http://bdistributed.com/          |  Personal Email: <email_removed>
_______________________________________________
cocoa-dev mailing list | <email_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

Related mailsAuthorDate
mlAre polymorphic objects possible? Wade Tregaskis Nov 2, 07:37
mlRe: Are polymorphic objects possible? John Hörnkvist Nov 2, 07:48
mlRe: Are polymorphic objects possible? Chris Hanson Nov 2, 08:03
mlRe: Are polymorphic objects possible? Wade Tregaskis Nov 2, 09:49
mlRe: Are polymorphic objects possible? Oliver Kennedy Nov 2, 16:55
mlRe: Are polymorphic objects possible? Chris Hanson Nov 2, 21:34