home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11831 < prev    next >
Encoding:
Internet Message Format  |  1992-07-30  |  2.7 KB

  1. Path: sparky!uunet!cs.utexas.edu!devnull!rgp
  2. From: rgp@mpd.tandem.com (Ramon Pantin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: tagged unions, an alternative to RTTI (Re: run-time type checking)
  5. Message-ID: <2157@devnull.mpd.tandem.com>
  6. Date: 31 Jul 92 02:34:12 GMT
  7. References: <BryL9q.K5I@watcgl.waterloo.edu> <1992Jul28.183746.24287@ucc.su.OZ.AU> <TMB.92Jul29125322@arolla.idiap.ch>
  8. Sender: news@devnull.mpd.tandem.com
  9. Organization: Tandem Computers, Micro-Products Division
  10. Lines: 58
  11.  
  12. In article <TMB.92Jul29125322@arolla.idiap.ch> tmb@idiap.ch writes:
  13. >In article <1992Jul28.183746.24287@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  14. >   >Consider the classic "List" class and "ListNode" class.
  15. >   >ListNode is a base class, and assume there are two flavors
  16. >   >of derived node.  We write a List that contains a mix of
  17. >   >these nodes out to a file; now we want to read the List back
  18. >   >in and reconstruct it.
  19. ...
  20. >       a) without some type information, it cant be done.
  21. >       b) without casting it can (and should :-) be done
  22. >
  23. >       union hetero {
  24. >           type1 *p1;
  25. >           type2 *p2;
  26. >       };
  27. >   And the list is homogeneous--all the elements are of type 'hetero'.
  28. >It would, in fact, be nice if C++ had a tagged union type, like Pascal
  29. >or SML. Something along the lines of:
  30. >
  31. >union hetero int {
  32. >case 1: type1 *p1;
  33. >case 2: type2 *p2;
  34. >};
  35. >
  36. >int f(hetero h) {
  37. >    switch(h) {
  38. >    case 1: return h.p1->foo();
  39. >    case 2: return h.p2->bar();
  40. >    }
  41. >}
  42. ...
  43.  
  44. The problem with this style of programming is that it goes
  45. against the object oriented programming principles.  Whenever
  46. you use unions of different representations of objects that
  47. are "similar" (instead of having a virtual base class and multiple
  48. classes derived from it) and the code that manipulates each type
  49. of object is located inside a bunch of switch statements spread all
  50. over your code you end up with traditional procedural programming
  51. not with OOP.
  52.  
  53. When you ask, where is all the code that manipualtes an "hetero"
  54. of "type1"? You have to go searching for all those switches.  What is
  55. even worse, when you need to _add_ anoter type to your "hetero" union
  56. (instead of simply deriving another class from your base class in OOP)
  57. then you have to find all the switches and change them.  Instead of
  58. writting and testing a new ".o" file that implements your new class
  59. you have to change all your old code that contains the switches and
  60. re-test it.
  61.  
  62. Another general problem with unions is that when you add another
  63. field to your union it might grow.  If you have instances of your
  64. unions stored somewhere (say on a file, from a previous incarnation
  65. of your program) then the new version of the program (where the union
  66. grew) will have know about the size change and have to deal with the
  67. old and new sizes of the union.
  68.  
  69. Ramon Pantin
  70.