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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!swrinde!sdd.hp.com!ux1.cso.uiuc.edu!cs.uiuc.edu!sparc0b!pjl
  3. From: pjl@cs.uiuc.edu (Paul Lucas)
  4. Subject: Re: Linked list of several classes.
  5. Message-ID: <C17x9C.DJy@cs.uiuc.edu>
  6. Sender: news@cs.uiuc.edu
  7. Organization: University of Illinois at Urbana-Champaign
  8. References: <1jlaifINNm4t@msuinfo.cl.msu.edu>
  9. Date: Thu, 21 Jan 1993 19:10:23 GMT
  10. Lines: 40
  11.  
  12. In <1jlaifINNm4t@msuinfo.cl.msu.edu> dulimart@cps.msu.edu (Hansye S. Dulimarta) writes:
  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.     Bingo!  You want run-time type-identification.  This is not
  45.     currently part of C++.  See Stroustrup, 13.5 for one of many,
  46.     many ways to do this.
  47.  
  48. -- 
  49.     - Paul J. Lucas
  50.       AT&T Bell Laboratories
  51.       Naperville, IL
  52.