Xref: utzoo comp.object:399 comp.lang.c++:5470 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!brunix!sdm From: sdm@brunix (Scott Meyers) Newsgroups: comp.object,comp.lang.c++ Subject: Re: Understanding the Object-Oriented Life-Cycle Message-ID: <20481@brunix.UUCP> Date: 11 Nov 89 03:28:39 GMT References: <5026@internal.Apple.COM> <315@shrike.AUSTIN.LOCKHEED.COM> <654@nastar.UUCP> <44454@sgi.sgi.com> Sender: news@brunix.UUCP Reply-To: sdm@norton.UUCP (Scott Meyers) Organization: Brown University Department of Computer Science Lines: 23 In article <44454@sgi.sgi.com> thant@horus.esd.sgi.com (Thant Tessman) writes: >Yes, you can implement generic lists and queues in C++ that don't care >what they contain, but the implementation is either not type safe, or >faked using macros. (See _Programming in C++_ by Dewhurst and Stark for >an interesting way to use macros to create generic container classes.) > >Another trick I (and probably many others) use is to inherit link-listability, >(or queueability) from a base class and then make the iterator function (e.g. >'next' or '++') a virtual and overload it to make it return the correct >type. This solution is easy if you know the things will only be in >one list at a time, and "possible" :-) if the things need to be in multiple >lists or queues at a time. Hmmm, what about using MI so that anything that will ever go on a list inherits from a class "Listable" that defines the appropriate virtual comparison operators? Then your lists would consist of pointers to objects of type Listable. It should be type-safe because only Listable objects can end up on lists, and all Listable objects have the necessary comparison operators appropriately defined. No macros needed, and having a single object on multiple lists is no problem. Am I overlooking something? Scott sdm@cs.brown.edu