• 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
Re: Are polymorphic objects possible?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Are polymorphic objects possible?


  • Subject: Re: Are polymorphic objects possible?
  • From: Wade Tregaskis <email@hidden>
  • Date: Sat, 2 Nov 2002 19:49:54 +1100

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?

Wade Tregaskis
_______________________________________________
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.

  • Prev by Date: control highlight color on 10.2
  • Next by Date: hand cursors
  • Previous by thread: Re: Are polymorphic objects possible?
  • Next by thread: Re: Are polymorphic objects possible?
  • Index(es):
    • Date
    • Thread