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