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

  1. Path: sparky!uunet!sun-barr!news2me.ebay.sun.com!exodus.Eng.Sun.COM!France.Sun.COM!obourdon
  2. From: obourdon@France.Sun.COM (Olivier Bourdon - Sun ICNC)
  3. Newsgroups: comp.lang.c++
  4. Subject: Generic types
  5. Date: 28 Aug 1992 14:03:06 GMT
  6. Organization: SunConnect
  7. Lines: 56
  8. Distribution: world
  9. Message-ID: <l9scgqINNh7u@exodus.Eng.Sun.COM>
  10. NNTP-Posting-Host: sunalps.france.sun.com
  11.  
  12. Let's assume I have two different types of objects (classes)
  13. //-------------------
  14. class complex {
  15. public:
  16.     int i,j;
  17.     ...
  18.     void Print();
  19. };
  20. //--------------------
  21. class string
  22. public:
  23.     char *str;
  24.     ...
  25.     void Print();
  26. };
  27.  
  28. and I also have a double linked list
  29. //-----------------
  30. class list {
  31.     void *element;
  32.     list *next, *prev;
  33.     void Print();
  34. };
  35.  
  36. then I woul like to do something like :
  37. void list::Print() {
  38.     while (this->next!=NULL)
  39.         this->elem->Print();
  40.         ...
  41. }
  42.  
  43. I know that it does not work AT COMPILE TIME because the Print function is not
  44.     a member function for void * type. It seems like I could create sur-classes
  45.     of list by removing the element field in the list class and adding :
  46. class stringlist {
  47.     string *element ;
  48.     ...
  49. };
  50.  
  51. but then I have to write 2 times the EXACTLY SAME CODE once for
  52. stringlist::Print and the other one for complexlist::Print
  53. and I would like to have only one piece of code so that if I have to
  54. modify it I do it only at one place
  55.  
  56. Thanks for any idea
  57. Olivier
  58.  
  59. -- 
  60. +---------------------------------+-----------------------------------+
  61. |Olivier BOURDON                  | Address : SUN MICROSYSTEMS        |
  62. |E-Mail : obourdon@France.Sun.COM |           International Center for|
  63. |Tel : +33 76 41 42 19            |           Network Computing       |
  64. |Fax : +33 76 41 42 41            |           32 av du Vieux Chene    |
  65. |                                 |           38 240 Meylan Zirst     |
  66. |member of OSI development team   |                  FRANCE           |
  67. +---------------------------------+-----------------------------------+
  68.