home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19743 < prev    next >
Encoding:
Text File  |  1993-01-22  |  2.7 KB  |  92 lines

  1. Xref: sparky comp.lang.c++:19743 comp.object:4992
  2. Path: sparky!uunet!dove!cme!sail.ncsl.nist.gov!schnee
  3. From: schnee@sail.ncsl.nist.gov (Rick Schneeman)
  4. Newsgroups: comp.lang.c++,comp.object
  5. Subject: Re: HELP : Array of objects of diff classes ??
  6. Message-ID: <20665@durer.cme.nist.gov>
  7. Date: 22 Jan 93 12:24:51 GMT
  8. References: <1993Jan19.112059.2882@ucc.su.OZ.AU>
  9. Sender: news@cme.nist.gov
  10. Followup-To: comp.lang.c++,comp.object
  11. Lines: 77
  12. Nntp-Posting-Host: sail.ncsl.nist.gov
  13. X-Newsreader: TIN [version 1.1 PL7]
  14.  
  15. John MAX Skaller (maxtal@extro.ucc.su.OZ.AU) wrote:
  16. :     Use a discriminated union. Oh, C++ doesn't have one,
  17. : so emulate one with an idiom.  Below I use pointers for 
  18. : illustration, you can put copies of the actual objects in
  19. : if you like (just get rid of appropriate *'s)
  20.  
  21. Well done example, I would add that this is termed a GENERIC
  22. which can be implemented in C++ in a variety of ways. In pre-2.0
  23. systems I would do it using macro expansion and the C++ preprocessor. 
  24. Below, I give a small example of macro expansion to iterate a list
  25. of classes.... 
  26.  
  27. //
  28. // TITLE:  GENERIC.H, My generic header file for macro expansion.
  29. // NAME:   Richard Schneeman
  30. // DATE:   December 11, 1990
  31. //
  32. //
  33. // "generic.h", this is my generic.h file for doing the macro expansion
  34. // that will allow a generic singly linked representation. This will
  35. // support the multiple class instances of customer, suppliers, and
  36. // invoices that are instantiated.
  37. //
  38. // Gslist, is a version of a generic singly linked list as defined in
  39. // Bjarne Stroudstrup's book C++ (section 7.3.5). This gslist will be
  40. // used to facilitate the STORABLE[T] phenomena that Eiffel allows to
  41. // represent generics/polymorphism.
  42. //
  43.  
  44. #include "slist.h"
  45.  
  46. #ifndef GENERICH
  47. #include <generic.h>    // don't bring in generic.h more that once
  48. #endif
  49.  
  50. #define gslist(type) name2(type, gslist)
  51. #define gslist_iterator(type) name2(type, gslist_iterator)
  52.  
  53. #define gslistdeclare(type)            \
  54.     class gslist(type) : slist            \
  55.     {                        \
  56.         int insert(type a)            \
  57.     {                    \
  58.         return slist::insert(ent(a));    \
  59.         }                    \
  60.     int append(type a)            \
  61.     {                    \
  62.         return slist::append(ent(a));    \
  63.         }                    \
  64.     type get()                \
  65.     {                    \
  66.         return type(slist::get());        \
  67.         }                    \
  68.     gslist(type)() {}            \
  69.     gslist(type)(type a) : (ent (a)) {}    \
  70.     ~gslist(type)()                \
  71.     {                    \
  72.         clear();                \
  73.         }                    \
  74.     }; //end generic class gslist        \
  75.     class gslist_iterator(type) : slist_iterator\
  76.     {                        \
  77.       gslist_iterator(type)(gslist(type) &s)    \
  78.       : ((slist &) s) {}            \
  79.       type operator()()                \
  80.       return type(slist_iterator::operator()());\
  81.     }; // end generic iterator class        \
  82.  
  83.  
  84. =======================
  85. Richard Schneeman, NIST
  86. Building 225, MS-B266
  87. Gaithersburg, MD 20899
  88. rschneeman@nist.gov
  89. =======================
  90.