Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!hplabs!hpl-opus!hpcc05!hpbbn!hpgnd!jr From: jr@hpgnd.grenoble.hp.com (Jean-Ren BOUVIER) Newsgroups: comp.lang.c++ Subject: How to define a List class Message-ID: <350001@hpgnd.grenoble.hp.com> Date: 3 Jun 91 15:07:44 GMT Organization: Hewlett-Packard, GND Lines: 37 I'm looking for ideas in the classical area of "list" packages. I want to define a "List" class which could be the base class of other objects so that these could then be put on linked lists. In a more C++-ish way, I'd like to have the following: class List { // details List *next; // I also accept solutions with "next" defined // as a (virtual) function. }; class SomeListableObject: public List /* may have other parents */ { // details }; // a few lines later ... SomeListableObject *list; // ... // assignment to list // ... while(list) { // do something list = list->next; } I DON'T want to consider solutions based on templates (List types) or generic classes - as I don't want to replicate code. My problem is that "next" holds (or returns) a pointer to a List, not to a SomeListableObject, i.e. the above code won't compile, unless I introduce a cast like: list = (SomeListableObject*)list->next; Is there a solution that avoids that cast and which doesn't use the preprocessor ?