home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12508 < prev    next >
Encoding:
Text File  |  1992-08-18  |  2.9 KB  |  65 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!caen!hellgate.utah.edu!fcom.cc.utah.edu!swillden
  3. From: swillden@news.ccutah.edu (Shawn Willden)
  4. Subject: Re: Object metamorphosis - can/should it be done?
  5. Message-ID: <1992Aug18.174916.19955@fcom.cc.utah.edu>
  6. Sender: news@fcom.cc.utah.edu
  7. Organization: University of Utah Computer Center
  8. X-Newsreader: Tin 1.1 PL3
  9. References: <9223115.11413@mulga.cs.mu.OZ.AU>
  10. Date: Tue, 18 Aug 92 17:49:16 GMT
  11. Lines: 52
  12.  
  13. fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
  14. : swillden@news.ccutah.edu (Shawn Willden) writes:
  15. : >I ran across some interesting Objective-C code the other day and I 
  16. : >wondered if there is some way to implement it in C++.  Then I wondered
  17. : >if it was a good idea to try.  Anyway, the concept is that an object
  18. : >can be asked to mutate into an object of a different type.  Under
  19. : >Objective-C's concept of an object, any object can mutate to essentially
  20. : >any other type because all messages are resolved at run-time.  In C++'s
  21. : >static system this is not possible, however, it seems to me that there 
  22. : >is at least one case in which it might be possible in C++ and maybe even 
  23. : >useful.  Given a base class pointer or reference to a derived class
  24. : >object, it should be possible to replace the derived class object with
  25. : >an object of another derived type.  
  26. : One major problem with your example code
  27. : >void Pupa::metamorphose() {
  28. : >    Pupa* old_me = this;
  29. : >    (Adult*)this = new Adult(*old_me);
  30. : >    delete old_me;
  31. : >}
  32. : is that "this" is passed by value, not by reference, so assignment to "this"
  33. : is not going to have the desired effect.
  34.  
  35. That makes sense.  In order to do what I want to I would have to find the
  36. original pointer and change it.  Thanks for your example.  It is essentially
  37. what I suggested, except that I was going to place all the functionality in
  38. the Butterfly class and use a flag to decide what the Butterfly currently
  39. "is".  I like your method better.  I believe that there was one error
  40. in your code, though, you derived Pupa and Adult from Butterfly rather
  41. than ButterflyStage. 
  42.  
  43. : Aside: this is another example where we want to do smart references.
  44. : A Butterfly is a smart reference to a ButterflyStage.
  45. : I could post code to show how to do it using overloading of operator
  46. : dot as per Jim Adcock's proposal, but since I am probably the ONLY
  47. : person in possession of a compiler that implements his proposal (I have
  48. : modified gcc 2.2.2 to do this) there isn't much point.
  49.  
  50. Would you mind making the diffs available?  I have considered modifying
  51. gcc to do this and a few other tricks (tagged pointers among them, though
  52. maybe I'll wait and let Max Skaller do that for me ;-> ).
  53.  
  54. : Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  55. : This .signature VIRUS is a self-referential statement that is true - but 
  56. : you will only be able to consistently believe it if you copy it to your own
  57. : .signature file!
  58.  
  59. Shawn Willden
  60. swillden@icarus.weber.edu
  61.