home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!uwm.edu!linac!uchinews!msuinfo!pixel.cps.msu.edu!dulimart
- From: dulimart@cps.msu.edu (Hansye S. Dulimarta)
- Newsgroups: comp.lang.c++
- Subject: Linked list of several classes.
- Date: 21 Jan 1993 04:58:55 GMT
- Organization: Department of Computer Science, Michigan State University
- Lines: 68
- Distribution: world
- Message-ID: <1jlaifINNm4t@msuinfo.cl.msu.edu>
- NNTP-Posting-Host: pixel.cps.msu.edu
- Originator: dulimart@pixel.cps.msu.edu
-
-
- To C++ gurus:
- I am having problem with defining a "generic" linked list. For instance,
- I would like to define a linked list of furnitures where each element can
- be either a Desk or a Table. The immediate "solution" that came to me was
- to define four classes:
-
- 1) List (for the list)
- 2) ListEl (for the list item)
- 3) Desk (derived from ListEl)
- 4) Table (derived from ListEl)
-
- Also, inside class List, I definde a method "search" that will return the
- pointer to a ListEl and another method "append" that will append a new
- element to the list.
-
- Function "append" behaves as I expected, that is, I can add element of
- class Desk and Table to the list like in the following:
-
- List inventory;
-
- inventory.append (new Desk(/*...*/));
- inventory.append (new Table(/*...*/));
-
- BUT......
- Whenever I do a 'search' I would like to get a return value of type
- 'Desk *' or 'Table *' depending on the element in the list that satisfies
- the search criterium. But the code, as it is written, always returns a
- 'ListEl *' and I lost the information whether the element is a Desk or a
- Table. I don't see a simple way to do this in my code.
-
- I really appreciate if some C++ gurus are willing to answer my question.
-
- My code, more or less, looks like the following:
-
- class ListEl;
-
- class List {
- private:
- ListEl *head;
- public:
- ListEl* search (/* some args here */);
- void append (ListEl *);
- };
-
- class ListEl {
- ListEl *next;
- friend class List;
- };
-
- class Desk : public ListEl {
- /* ... */
- };
-
- class Table : public ListEl {
- /* ... */
- };
-
-
- |_|_|_|_| Hans Dulimarta [dulimart@cps.msu.edu] |_|_|_|_|
- |_|_|_| Pattern Recognition & Image Processing Laboratory |_|_|_|
- |_|_| Department of Computer Science |_|_|
- |_| Michigan State University |_|
- --
- |_|_|_|_| _|_|_ |_|_|_|_| Hans Dulimarta [dulimart@cps.msu.edu]
- |_|_|_| _|_| |_|_ |_|_|_| Pattern Recognition & Image Processing Laboratory
- |_|_| _|_|_ X _|_|_ |_|_| Department of Computer Science
- |_| |_|_|_|_|_|_|_| |_| Michigan State University
-