Re: Are polymorphic objects possible?
Re: Are polymorphic objects possible?
- Subject: Re: Are polymorphic objects possible?
- From: Oliver Kennedy <email@hidden>
- Date: Sat, 2 Nov 2002 10:55:38 -0500
On Saturday, Nov 2, 2002, at 10:13 US/Eastern,
email@hidden wrote:
>
>> Is it possible for an object to change at runtime what it appears to
>
>> be, to the rest of the program? For instance, if I have two types of
>
>> object, say WTFoo and WTBar, can I make another one under some name
>
>> (WTMorphy, for example) which can pretend to be WTFoo & WTBar, as it
>
>> likes? i.e. I get an instance of WTMorphy, tell it to be a WTFoo,
>
>> then at some later point it changes itself to a WTBar?
>
>>
>
>> I know this sounds strange, and I'm not pretending it's necessarily a
>
>> good idea to ever actually do, but I'm curious if it's possible. I
>
>> can think of a couple of instances where it might be worth testing
>
>> this, depending on how well it can be done.
>
>
>
> 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?
>
>
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?
Since the conversion from selector (being based only on the method
name) to physical address happens at runtime, the answer is yes. This
is one of the fundamental ideas of ObjC. It's responsible for most of
the flexibility present therein.
-Oliver
6*9 = 42 (in base 13)
AIM:xthemage
http://home.talkcity.com/technologyway/macperson/
http://homepage.mac.com/xthemage/publickey.txt
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS/S/M/P d?(-) s+: a? C++$ UXLBS*+++ P+@ L+(+++)
E++$>+++ W++@ N+ !o K+ w--- !O@ M++$ V? PS+ PE-
Y+ PGP++@ t+++ 5>+(++) X@ R++>+++$ tv+>++ b+
DI++++ UF+++ D G++ e*>+++++(+++++$) h!> r! y-
------END GEEK CODE BLOCK------
_______________________________________________
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.