Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!hplabs!hpcc05!hpbbn!hpgnd!jr From: jr@hpgnd.grenoble.hp.com (Jean-Ren BOUVIER) Newsgroups: comp.lang.c++ Subject: How to define a List class (summary) Message-ID: <350002@hpgnd.grenoble.hp.com> Date: 11 Jun 91 10:59:39 GMT Organization: Hewlett-Packard, GND Lines: 68 A while ago, I posted a note about how to define a list class without using the preprocessor, generic classes and casts. I want to thank all of you who've sent me suggestions and summarize your inputs. As Kevin Coombes states it, there seems to be no solution: |From: kevin@math.lsa.umich.edu | |You ask (essentially): can I create a list class to hold |arbitrary kinds of objects, without templates, without |using the preprocessor, and without explicit casts? |The answer is no. Personally, I've decided on the explicit |cast approach, since my compiler doesn't support templates |and I figure I'd rather reuse the same code without doubling |the size of the executable file. I may change my mind, though.... |Best, | Kevin Coombes I received a number of solutions, but I should have explained more precisely what my goals were: 1- I don't want to replicate code which has no reason to be duplicated, and I don't want derived classes writers to have to provide code specific to list handling. 2- I want to have a natural interface, i.e. being able to access my next element in a list by using something like "list->next" or "list->next()", the latter being easily made similar to the former by #defining "next" to be "next()" (I know, I explicitely forbade the use of the preprocessor :-)). Still, quite a few proposed solutions are interesting (I'll steal a few ideas from them) but they don't meat my goals, most of them because they define (member) functions to scan the list. One proposal is quite close to meeting my requirements, it is the one submitted by Amit Jayant Patel (Paul Connors had a similar suggestion): |From: Amit Jayant Patel | class List; | typedef List *ListPtr; | | class List { | // no List *next; | virtual ListPtr& Next() = 0; | }; | | class SomeListableObject: public List { | SomeListableObject *next; | ListPtr& Next() { return next; } | }; //.... | SomeListableObject *list; | while( list ) { | list = list -> next; | } However, it fails to fully reach goal #1, as the derived class must provide the virtual "next()" function along with a "next" pointer. On the other hand, the code required from the derived class writer is very small. David Jones also sent me a full list package which doesn't require its user to write list handling code, but which doesn't meet my interface requirements. thanks for your help and time, Jean-Rene.