home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16235 < prev    next >
Encoding:
Text File  |  1992-11-14  |  2.6 KB  |  73 lines

  1. Path: sparky!uunet!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
  2. From: tmb@arolla.idiap.ch (Thomas M. Breuel)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Discriminating Variant Records Without switch() ??
  5. Date: 13 Nov 92 20:52:54
  6. Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
  7.     Perceptive)
  8. Lines: 57
  9. Message-ID: <TMB.92Nov13205254@arolla.idiap.ch>
  10. References: <BAB.92Nov10073324@se39.wg2.waii.com> <1992Nov11.144253.26415@bnr.ca>
  11.     <BAB.92Nov12074430@se39.wg2.waii.com> <1992Nov12.190518.20213@bnr.ca>
  12. Reply-To: tmb@idiap.ch
  13. NNTP-Posting-Host: arolla.idiap.ch
  14. In-reply-to: holiday@bnr.ca's message of Thu, 12 Nov 1992 19:05:18 GMT
  15.  
  16. In article <1992Nov12.190518.20213@bnr.ca> holiday@bnr.ca (Matthew Holiday) writes:
  17.  
  18.    One thing that would help would be the ability to "transmute" an
  19.    object into a subclass of the object's current class.  That is, the
  20.    factory could create a top-level object.  After some processing,
  21.    this object could determine that it's really more specialized, and
  22.    turn itself into a subclass object.  Take a trivial example: a
  23.    shape is constructed using some points.  Further processing reveals
  24.    that the points define a rectangle, so the shape converts itself
  25.    into a rectangle.  Yet more processing reveals that the rectangle
  26.    is in fact a square -- so it becomes an object of type square,
  27.    which was derived from rectangle and shape.  One of the problems
  28.    with this downward transmutation is that if you simply replace the
  29.    object, it will have a new address, so you have to update all
  30.    pointers to the object every time it is replaced.  You can't
  31.    necessarily transmute in place, because the new subclass object may
  32.    be too large to fit into its current space in memory.  In addition
  33.    to this "transmute" capability, downcasting is also useful (see
  34.    Stroutrup's RTTI papers).
  35.  
  36. You can "transmute in place" if you put an object in between:
  37.  
  38. struct GeneralShape;
  39. struct Point:GeneralShape;
  40. struct Rectangle:GeneralShape;
  41.  
  42. enum {SHAPE_NONE,SHAPE_POINT,SHAPE_RECTANGLE} ShapeTag;
  43.  
  44. class TransmutableShape {
  45. private:
  46.     int which_shape;
  47.     GeneralShape *the_shape;
  48. public:
  49.     void operator=(Point *obj) {
  50.         the_shape = obj;
  51.         which_shape = SHAPE_POINT;
  52.     }
  53.     void operator=(Rectangle *obj) {
  54.         the_shape = obj;
  55.         which_shape = SHAPE_RECTANGLE;
  56.     }
  57.     // ...
  58.     operator Point *() {
  59.         if(which_shape!=SHAPE_POINT) return 0;
  60.         else return (Point *)the_shape;
  61.     }
  62.     operator Rectangle *() {
  63.         if(which_shape!=SHAPE_RECTANGLE) return 0;
  64.         else return (Rectangle *)the_shape;
  65.     }
  66.     // ...
  67. };
  68.  
  69. This is essentially what the implementation would have to
  70. do anyway.
  71.  
  72.                 Thomas.
  73.