Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uunet!samsung!zaphod.mps.ohio-state.edu!mips!apple!voder!decvax!roger From: roger@decvax.UUCP (Roger H. Scott) Newsgroups: comp.lang.c++ Subject: Re: Chameleon objects (calling virtual functions from constructors) Keywords: virtual functions, constructor functions Message-ID: <64@espol.decvax.UUCP> Date: 3 Jan 90 21:45:50 GMT References: <11266@csli.Stanford.EDU> <47479d37.12160@espol> <1727@osc.COM> Reply-To: roger@procase.UUCP (Roger H. Scott) Organization: proCASE Corporation, Santa Clara, CA Lines: 59 In article <1727@osc.COM> strick@osc.com (henry strickland) writes: >In article <47479d37.12160@espol> roger@procase.UUCP (Roger H. Scott) writes: >> >>[Digression #2 - for Language Lawyers only] >>"Derived *(*)()" [pointer to function returning pointer to Derived] >>should be type compatible with "Base *(*)()" [pointer to function >>returning pointer to Base]. These types were compatible in 1.2. > >In 1.2 you did get away with this, because there were no >multiply inherited bases buried within an object -- all the >bases overlaid each other, starting at the same address. > >... >The following program demonstrates a simple example where >(Base*)&x and (Derived*)&x are not the same absolute value, >for a Derived x. ============================================== extern "C" void printf( char const*, ... ); class Base { int b; }; class Other { int o; }; class Derived : public Other, public Base { int d; }; Derived x; main(int, char*[] ) { ::printf("%x %x\n", (Base*) &x, (Derived*) &x); return 0; } typedef Base *(*PFPBase)(); typedef Derived *(*PFPDerived)(); Base* BaseOfX() { return &x; } void func() { PFPBase f= BaseOfX; Base* mumble= (*f)(); PFPDerived g= BaseOfX; // error: // bad initializer type Base *(*)() // for g ( PFPDerived expected) // the above would lead to the next line not // working right, if the compiler allowed // it, because fleezle would get the absolute // value (Base*)&x rather than // the absolute value (Derived*)&x . ### Of course this is wrong - you are going the wrong way! I claimed that "Derived *(*)()" should be compatible with "Base *(*)()", not vice versa as you are doing here. With this clarification do you still maintain that I am wrong and that the 2.0 behavior is right and/or necessary? Derived* fleezle= (*g)(); } >