home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
- From: tmb@arolla.idiap.ch (Thomas M. Breuel)
- Newsgroups: comp.lang.c++
- Subject: Re: Discriminating Variant Records Without switch() ??
- Date: 13 Nov 92 20:52:54
- Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
- Perceptive)
- Lines: 57
- Message-ID: <TMB.92Nov13205254@arolla.idiap.ch>
- References: <BAB.92Nov10073324@se39.wg2.waii.com> <1992Nov11.144253.26415@bnr.ca>
- <BAB.92Nov12074430@se39.wg2.waii.com> <1992Nov12.190518.20213@bnr.ca>
- Reply-To: tmb@idiap.ch
- NNTP-Posting-Host: arolla.idiap.ch
- In-reply-to: holiday@bnr.ca's message of Thu, 12 Nov 1992 19:05:18 GMT
-
- In article <1992Nov12.190518.20213@bnr.ca> holiday@bnr.ca (Matthew Holiday) writes:
-
- One thing that would help would be the ability to "transmute" an
- object into a subclass of the object's current class. That is, the
- factory could create a top-level object. After some processing,
- this object could determine that it's really more specialized, and
- turn itself into a subclass object. Take a trivial example: a
- shape is constructed using some points. Further processing reveals
- that the points define a rectangle, so the shape converts itself
- into a rectangle. Yet more processing reveals that the rectangle
- is in fact a square -- so it becomes an object of type square,
- which was derived from rectangle and shape. One of the problems
- with this downward transmutation is that if you simply replace the
- object, it will have a new address, so you have to update all
- pointers to the object every time it is replaced. You can't
- necessarily transmute in place, because the new subclass object may
- be too large to fit into its current space in memory. In addition
- to this "transmute" capability, downcasting is also useful (see
- Stroutrup's RTTI papers).
-
- You can "transmute in place" if you put an object in between:
-
- struct GeneralShape;
- struct Point:GeneralShape;
- struct Rectangle:GeneralShape;
-
- enum {SHAPE_NONE,SHAPE_POINT,SHAPE_RECTANGLE} ShapeTag;
-
- class TransmutableShape {
- private:
- int which_shape;
- GeneralShape *the_shape;
- public:
- void operator=(Point *obj) {
- the_shape = obj;
- which_shape = SHAPE_POINT;
- }
- void operator=(Rectangle *obj) {
- the_shape = obj;
- which_shape = SHAPE_RECTANGLE;
- }
- // ...
- operator Point *() {
- if(which_shape!=SHAPE_POINT) return 0;
- else return (Point *)the_shape;
- }
- operator Rectangle *() {
- if(which_shape!=SHAPE_RECTANGLE) return 0;
- else return (Rectangle *)the_shape;
- }
- // ...
- };
-
- This is essentially what the implementation would have to
- do anyway.
-
- Thomas.
-