home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:19743 comp.object:4992
- Path: sparky!uunet!dove!cme!sail.ncsl.nist.gov!schnee
- From: schnee@sail.ncsl.nist.gov (Rick Schneeman)
- Newsgroups: comp.lang.c++,comp.object
- Subject: Re: HELP : Array of objects of diff classes ??
- Message-ID: <20665@durer.cme.nist.gov>
- Date: 22 Jan 93 12:24:51 GMT
- References: <1993Jan19.112059.2882@ucc.su.OZ.AU>
- Sender: news@cme.nist.gov
- Followup-To: comp.lang.c++,comp.object
- Lines: 77
- Nntp-Posting-Host: sail.ncsl.nist.gov
- X-Newsreader: TIN [version 1.1 PL7]
-
- John MAX Skaller (maxtal@extro.ucc.su.OZ.AU) wrote:
- :
- : Use a discriminated union. Oh, C++ doesn't have one,
- : so emulate one with an idiom. Below I use pointers for
- : illustration, you can put copies of the actual objects in
- : if you like (just get rid of appropriate *'s)
- :
-
- Well done example, I would add that this is termed a GENERIC
- which can be implemented in C++ in a variety of ways. In pre-2.0
- systems I would do it using macro expansion and the C++ preprocessor.
- Below, I give a small example of macro expansion to iterate a list
- of classes....
-
- //
- // TITLE: GENERIC.H, My generic header file for macro expansion.
- // NAME: Richard Schneeman
- // DATE: December 11, 1990
- //
- //
- // "generic.h", this is my generic.h file for doing the macro expansion
- // that will allow a generic singly linked representation. This will
- // support the multiple class instances of customer, suppliers, and
- // invoices that are instantiated.
- //
- // Gslist, is a version of a generic singly linked list as defined in
- // Bjarne Stroudstrup's book C++ (section 7.3.5). This gslist will be
- // used to facilitate the STORABLE[T] phenomena that Eiffel allows to
- // represent generics/polymorphism.
- //
-
- #include "slist.h"
-
- #ifndef GENERICH
- #include <generic.h> // don't bring in generic.h more that once
- #endif
-
- #define gslist(type) name2(type, gslist)
- #define gslist_iterator(type) name2(type, gslist_iterator)
-
- #define gslistdeclare(type) \
- class gslist(type) : slist \
- { \
- int insert(type a) \
- { \
- return slist::insert(ent(a)); \
- } \
- int append(type a) \
- { \
- return slist::append(ent(a)); \
- } \
- type get() \
- { \
- return type(slist::get()); \
- } \
- gslist(type)() {} \
- gslist(type)(type a) : (ent (a)) {} \
- ~gslist(type)() \
- { \
- clear(); \
- } \
- }; //end generic class gslist \
- class gslist_iterator(type) : slist_iterator\
- { \
- gslist_iterator(type)(gslist(type) &s) \
- : ((slist &) s) {} \
- type operator()() \
- return type(slist_iterator::operator()());\
- }; // end generic iterator class \
-
-
- =======================
- Richard Schneeman, NIST
- Building 225, MS-B266
- Gaithersburg, MD 20899
- rschneeman@nist.gov
- =======================
-