Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!mcsun!hp4nl!kunivv1!atcmpe!leo From: leo@atcmp.nl (Leo Willems) Newsgroups: comp.lang.c++ Subject: Re: What to do about generics and polymorphism in C++ Message-ID: <574@atcmpe.atcmp.nl> Date: 25 Mar 90 16:51:53 GMT References: <152@dumbcat.UUCP> Organization: AT Computing, Nijmegen, The Netherlands Lines: 40 From article <152@dumbcat.UUCP>, by marc@dumbcat.UUCP (Marco S Hyman): > In article <16808@well.sf.ca.us> nagle@well.UUCP (John Nagle) writes: > > Let's start a discussion about the problem of how to achieve > > generic objects (ones with types as parameters, at least at compile > > time) in C++. Some possibilities: > > > > 1. Explicitly use "pointer to void". Works, but violates > > type safety and requires much repetitive writing. > > Doesn't always work. In the case of multiple inheritance pointer adjustment > void * a different type than was placed in the void pointer, things break. Stuff deleted > void * generic; > D something; > B1 * b1 = &something; > B2 * b2; > > ++b1->i1; > generic = b1; // no adjustment > b2 = generic; // no adjustment Under C++ 1.2 the last sentence will give a "bad assigment type" error. As I understand it, assignment to void* must be casted in C++ (which is different in ANSI-C) thereby bringing type-information back. Isn't this true in C++ 2.0? (I have a lot of code (of the preprocessor- generic type) which will break if type-information is lost (using a cast)). If you apply a cast in the example does it work correct then? (If it works, is it legal or just luck? Andrew Koenig told me a long time ago it is legal, but that was in the 1.2 darkages, in which I still live :-)) Leo Willems Internet: leo@atcmp.nl AT Computing UUCP: mcvax!hp4nl!kunivv1!atcmpe!leo P. O. Box 1428 6501 BK Nijmegen Phone: +31-80-566880 The Netherlands Fax: +31-80-555887