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

  1. Path: sparky!uunet!cs.utexas.edu!uwm.edu!linac!uchinews!msuinfo!pixel.cps.msu.edu!dulimart
  2. From: dulimart@cps.msu.edu (Hansye S. Dulimarta)
  3. Newsgroups: comp.lang.c++
  4. Subject: Linked list of several classes.
  5. Date: 21 Jan 1993 04:58:55 GMT
  6. Organization: Department of Computer Science, Michigan State University
  7. Lines: 68
  8. Distribution: world
  9. Message-ID: <1jlaifINNm4t@msuinfo.cl.msu.edu>
  10. NNTP-Posting-Host: pixel.cps.msu.edu
  11. Originator: dulimart@pixel.cps.msu.edu
  12.  
  13.  
  14. To C++ gurus:
  15.   I am having problem with defining a "generic" linked list. For instance,
  16. I would like to define a linked list of furnitures where each element can
  17. be either a Desk or a Table. The immediate "solution" that came to me was
  18. to define four classes:
  19.  
  20.     1) List (for the list)
  21.     2) ListEl (for the list item)
  22.     3) Desk (derived from ListEl)
  23.     4) Table (derived from ListEl)
  24.  
  25. Also, inside class List, I definde a method "search" that will return the
  26. pointer to a ListEl and another method "append" that will append a new
  27. element to the list. 
  28.  
  29. Function "append" behaves as I expected, that is, I can add element of
  30. class Desk and Table to the list like in the following:
  31.  
  32.     List inventory;
  33.  
  34.     inventory.append (new Desk(/*...*/));
  35.     inventory.append (new Table(/*...*/));
  36.  
  37. BUT......
  38.   Whenever I do a 'search' I would like to get a return value of type
  39.   'Desk *' or 'Table *' depending on the element in the list that satisfies
  40.   the search criterium. But the code, as it is written, always returns a
  41.   'ListEl *' and I lost the information whether the element is a Desk or a
  42.   Table. I don't see a simple way to do this in my code.
  43.  
  44. I really appreciate if some C++ gurus are willing to answer my question.
  45.  
  46. My code, more or less, looks like the following:
  47.  
  48. class ListEl;
  49.  
  50. class List {
  51. private:
  52.   ListEl *head;
  53. public:
  54.   ListEl* search (/* some args here */);
  55.   void append (ListEl *);
  56. };
  57.  
  58. class ListEl {
  59.   ListEl *next;
  60.   friend class List;
  61. };
  62.  
  63. class Desk : public ListEl {
  64.   /* ... */
  65. };
  66.  
  67. class Table : public ListEl {
  68.   /* ... */
  69. };
  70.  
  71.  
  72.  |_|_|_|_|      Hans Dulimarta [dulimart@cps.msu.edu]        |_|_|_|_|
  73.    |_|_|_| Pattern Recognition & Image Processing Laboratory |_|_|_|
  74.      |_|_|         Department of Computer Science            |_|_|
  75.        |_|            Michigan State University              |_|
  76. -- 
  77. |_|_|_|_|  _|_|_ |_|_|_|_| Hans Dulimarta [dulimart@cps.msu.edu]
  78. |_|_|_|  _|_| |_|_ |_|_|_| Pattern Recognition & Image Processing Laboratory
  79. |_|_|  _|_|_ X _|_|_ |_|_| Department of Computer Science
  80. |_|   |_|_|_|_|_|_|_|  |_| Michigan State University
  81.