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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!usc!sdd.hp.com!ux1.cso.uiuc.edu!m.cs.uiuc.edu!sunb10.cs.uiuc.edu!sparc6.cs.uiuc.edu!pjl
  3. From: pjl@sparc6.cs.uiuc.edu (Paul Lucas)
  4. Subject: Re: Generic types
  5. Message-ID: <1992Aug29.163433.10007@sunb10.cs.uiuc.edu>
  6. Sender: news@sunb10.cs.uiuc.edu
  7. Organization: University of Illinois at Urbana-Champaign
  8. References: <l9scgqINNh7u@exodus.Eng.Sun.COM>
  9. Date: Sat, 29 Aug 1992 16:34:33 GMT
  10. Lines: 74
  11.  
  12. In <l9scgqINNh7u@exodus.Eng.Sun.COM> obourdon@France.Sun.COM (Olivier Bourdon - Sun ICNC) writes:
  13.  
  14. >Let's assume I have two different types of objects (classes)
  15. >//-------------------
  16. >class complex {
  17. >public:
  18. >    int i,j;
  19. >    ...
  20. >    void Print();
  21. >};
  22. >//--------------------
  23. >class string
  24. >public:
  25. >    char *str;
  26. >    ...
  27. >    void Print();
  28. >};
  29.  
  30. >and I also have a double linked list
  31. >//-----------------
  32. >class list {
  33. >    void *element;
  34. >    list *next, *prev;
  35. >    void Print();
  36. >};
  37.  
  38. >then I woul like to do something like :
  39. >void list::Print() {
  40. >    while (this->next!=NULL)
  41. >        this->elem->Print();
  42. >        ...
  43. >}
  44.  
  45. >I know that it does not work AT COMPILE TIME because the Print function is not
  46. >    a member function for void * type. It seems like I could create sur-classes
  47. >    of list by removing the element field in the list class and adding :
  48. >class stringlist {
  49. >    string *element ;
  50. >    ...
  51. >};
  52.  
  53. >but then I have to write 2 times the EXACTLY SAME CODE once for
  54. >stringlist::Print and the other one for complexlist::Print
  55. >and I would like to have only one piece of code so that if I have to
  56. >modify it I do it only at one place
  57.  
  58. *****>    You have to have a base Link class that has Print() in it from which
  59.     all other things that are to be on the list have to be derived from.
  60.     (This make for what is called an "intrusive list.")  Print() has to
  61.     be virtual.
  62.  
  63.     You can also decide whether you want the list to be "managed" or
  64.     not, i.e., when the list goes away, does it destroy the elements
  65.     also.  Stroustrup, 2nd ed., has examples of this very thing.  I've
  66.     done things similar to:
  67.  
  68.         class Link {    // not List since it's just a link in a list
  69.             Link *prev, *next;
  70.             Link *element;
  71.         public:
  72.             // ...
  73.             virtual void Print() = 0;
  74.         };
  75.  
  76.         class Foo : public Link {
  77.             // ...
  78.         public:
  79.             // ...
  80.             void Print();
  81.         };
  82. -- 
  83.     - Paul J. Lucas                University of Illinois    
  84.       AT&T Bell Laboratories        at Urbana-Champaign
  85.       Naperville, IL            pjl@cs.uiuc.edu
  86.