home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13021 < prev    next >
Encoding:
Text File  |  1992-08-29  |  2.2 KB  |  102 lines

  1. Path: sparky!uunet!ogicse!usenet.coe.montana.edu!decwrl!csus.edu!borland.com!pete
  2. From: pete@genghis.borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Generic types
  5. Message-ID: <1992Aug28.163946.497@genghis.borland.com>
  6. Date: 28 Aug 92 16:39:46 GMT
  7. Article-I.D.: genghis.1992Aug28.163946.497
  8. References: <l9scgqINNh7u@exodus.Eng.Sun.COM>
  9. Sender: news@borland.com (News Admin)
  10. Organization: Borland International
  11. Lines: 88
  12. Originator: pete@genghis.borland.com
  13.  
  14. In article <l9scgqINNh7u@exodus.Eng.Sun.COM> obourdon@France.Sun.COM (Olivier Bourdon - Sun ICNC) writes:
  15. >Let's assume I have two different types of objects (classes)
  16. >//-------------------
  17. >class complex {
  18. >public:
  19. >    int i,j;
  20. >    ...
  21. >    void Print();
  22. >};
  23. >//--------------------
  24. >class string
  25. >public:
  26. >    char *str;
  27. >    ...
  28. >    void Print();
  29. >};
  30. >
  31. >and I also have a double linked list
  32. >//-----------------
  33. >class list {
  34. >    void *element;
  35. >    list *next, *prev;
  36. >    void Print();
  37. >};
  38. >
  39. >then I woul like to do something like :
  40. >void list::Print() {
  41. >    while (this->next!=NULL)
  42. >        this->elem->Print();
  43. >        ...
  44. >}
  45. >
  46.  
  47.     There are two (at least) ways to do this, depending on what this list
  48. class is really supposed to do.  If each instance of a list will only hold one
  49. of these types of objects, then the way to do it is with a template:
  50.  
  51.     class ListBase
  52.     {
  53.     protected:
  54.         void *element;
  55.         list *next, *prev;
  56.     public:
  57.         void insert( void * );
  58.         virtual void Print() = 0;
  59.     };
  60.  
  61.     template <class T> class List : public ListBase
  62.     {
  63.     public:
  64.         void insert( T *item ) { ListBase::insert( item ); }
  65.         void Print();
  66.     };
  67.  
  68.     template <class T> void List<T>::Print()
  69.     {
  70.     ListBase *current = this;
  71.     while( current != 0 )
  72.         {
  73.         cout << *(T *)(current->element) << endl;
  74.         current = current->next;
  75.         }
  76.     }
  77.  
  78. (Although I'd probably put the iteration in the base class, and use a function
  79. to actually print the element).
  80.     Or, if this list is going to have to hold both types simultaneously,
  81. then both types must be derived from a common base class:
  82.  
  83.     class ListElement
  84.     {
  85.     public:
  86.         void show();
  87.     };
  88.  
  89.     class Complex : public ListElement
  90.     {
  91.     ...
  92.     };
  93.  
  94.     class String : public ListElement
  95.     {
  96.     ...
  97.     };
  98.  
  99.     Now the list class should hold pointers to ListElements rather than
  100. pointers to void.
  101.     --Pete
  102.