Path: utzoo!attcan!uunet!munnari.oz.au!uhccux!ames!pacbell!hoptoad!hsfmsh!dumbcat!marc From: marc@dumbcat.UUCP (Marco S Hyman) Newsgroups: comp.lang.c++ Subject: Re: What to do about generics and polymorphism in C++ Message-ID: <153@dumbcat.UUCP> Date: 27 Mar 90 06:34:00 GMT References: <152@dumbcat.UUCP> <574@atcmpe.atcmp.nl> Organization: MH Software, Hayward, Ca. Lines: 30 In article <574@atcmpe.atcmp.nl> leo@atcmp.nl (Leo Willems) writes: > From article <152@dumbcat.UUCP>, by marc@dumbcat.UUCP (Marco S Hyman): > > void * generic; > > D something; > > B1 * b1 = &something; > > B2 * b2; > > > > ++b1->i1; > > generic = b1; // no adjustment > > b2 = generic; // no adjustment > > 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 :-)) G++ (the version I used to check the above example) does not require the cast. My cfront 2.0 hasn't arived for this machine yet so I can't test it, but am under the impression that when you use a case you are only telling the compiler that you know what you are doing. If you specify b2 = (B2 *) generic; you are telling the compiler that you know generic is a B2. No adjustment will be made and your code breaks. The moral is *don't* use void* for generics. // marc -- // {ames,decwrl,sun}!pacbell!dumbcat!marc // pacbell!dumbcat!marc@lll-winken.llnl.gov