Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!zaphod.mps.ohio-state.edu!usc!ucsd!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: <152@dumbcat.UUCP> Date: 23 Mar 90 05:37:22 GMT References: <16808@well.sf.ca.us> Organization: MH Software, Hayward, Ca. Lines: 50 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 is not performed when assigning to a void pointer. When you take from a void * a different type than was placed in the void pointer, things break. Example: #include class B1 { public: int i1; B1(): i1(0) {}; }; class B2 { public: int i2; B2(): i2(0) {}; }; class D: public B1, public B2 { public: int d; D(): d(0) {}; }; main() { void * generic; D something; B1 * b1 = &something; B2 * b2; ++b1->i1; generic = b1; // no adjustment b2 = generic; // no adjustment printf( "%d\n", b2->i2 ); } The output will be 1, not zero. The compiler assumes you know what you're doing when you use void *. Ouch. // marc -- // {ames,decwrl,sun}!pacbell!dumbcat!marc // pacbell!dumbcat!marc@lll-winken.llnl.gov