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