home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16197 < prev    next >
Encoding:
Internet Message Format  |  1992-11-13  |  3.3 KB

  1. Path: sparky!uunet!know!cass.ma02.bull.com!mips2!news.bbn.com!usc!cs.utexas.edu!convex!news.utdallas.edu!corpgate!crchh327!bnr.ca!holiday
  2. From: holiday@bnr.ca (Matthew Holiday)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Discriminating Variant Records Without switch() ??
  5. Message-ID: <1992Nov12.190518.20213@bnr.ca>
  6. Date: 12 Nov 92 19:05:18 GMT
  7. References: <BAB.92Nov10073324@se39.wg2.waii.com> <1992Nov11.144253.26415@bnr.ca> <BAB.92Nov12074430@se39.wg2.waii.com>
  8. Sender: holiday@crchh82 (Matthew Holiday)
  9. Reply-To: holiday@bnr.ca
  10. Organization: Bell-Northern Research, Richardson, TX
  11. Lines: 48
  12. Nntp-Posting-Host: crchh82
  13.  
  14. In article <BAB.92Nov12074430@se39.wg2.waii.com>, bab@se39.wg2.waii.com (Brian Button) writes:
  15. |> 
  16. |> In article <BAB.92Nov10073324@se39.wg2.waii.com>, bab@se39.wg2.waii.com (Brian Button) writes:
  17. |> |> Is there a simple, OOP based way to discriminate between variant
  18. |> |> records? I saw an article in C Users Journal last month titled "Switch
  19. |> |> Deemed Harmful to C++" or something like that, but I couldn't figure
  20. |> |> out how to do it any other way.
  21. |> 
  22. |> 
  23. |> ->"Matthew" == Matthew Holiday <holiday@bnr.ca> writes:
  24. |>    Matthew> switch statement! :-) There ought to be a convenient way
  25. |>    Matthew> to create objects from network messages, i.e., some way to
  26. |>    Matthew> register message types and their matching class types,
  27. |>    Matthew> such that for every incoming message, its body (an array
  28. |>    Matthew> of bytes) gets turned into the correct object, which would
  29. |>    Matthew> be derived from a root generic message object.
  30. |> 
  31. |> There is such a way using factory classes such as the RogueWave
  32. |> RWFactory class. The problem I'm having is figuring out how to deal
  33. |> with these objects after they are already created. Since there's no
  34. |> common set of operations which are performed on each of the objects,
  35. |> there is no set of methods which may be made virtual and overloaded.
  36. |> Herein lies the problem. This seems to point towards using special
  37. |> purpose code for each class, which would necessitate using a switch.
  38. |> 
  39. |> Have I painted myself into a corner or is there some way out. How are
  40. |> the rest of you OOP programmers dealing with cases like this?
  41. |> 
  42.  
  43. One thing that would help would be the ability to "transmute" an object into
  44. a subclass of the object's current class.  That is, the factory could create
  45. a top-level object.  After some processing, this object could determine that
  46. it's really more specialized, and turn itself into a subclass object.  Take a
  47. trivial example: a shape is constructed using some points.  Further processing
  48. reveals that the points define a rectangle, so the shape converts itself into
  49. a rectangle.  Yet more processing reveals that the rectangle is in fact a
  50. square -- so it becomes an object of type square, which was derived from
  51. rectangle and shape.  One of the problems with this downward transmutation is
  52. that if you simply replace the object, it will have a new address, so you have
  53. to update all pointers to the object every time it is replaced.  You can't
  54. necessarily transmute in place, because the new subclass object may be too
  55. large to fit into its current space in memory.  In addition to this "transmute"
  56. capability, downcasting is also useful (see Stroutrup's RTTI papers).
  57.  
  58. -- 
  59. Matt Holiday                                      #include <std/disclaimer>
  60. holiday@bnr.ca
  61. BNR Richardson, TX
  62.