leo@atcmp.nl (Leo Willems) (09/26/89)
In the following program a class object its address is put in to a void pointer. After restoring this pointer into a pointer of the base class, typeinfo seems to be restored: class B{ public: virtual void prt(){ cout << "a B\n"; } }; class D : public B{ public: void prt(){ cout << "a D\n"; } }; typedef void (B::*PF)(); main() { PF pf = B::prt; B* bp = new D; void* x = bp; // loosing type info bp = (B*) x; // returning type info? (bp->*pf)(); // bp->D::prt() ! // which is what I want (and expect) } (It works under g++ 1.35.0, it does not compile under 1.2E glockenspiel due to a ternary operator problem in the generated C code. After patching that to an if statement it compiles but calls bp->B::prt(), it lost typeinfo somewhere) Is this example program considered to be legal in C++ ( rel. 12.E and up)? 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
dog@cbnewsl.ATT.COM (edward.n.schiebel) (09/27/89)
From article <536@atcmpe.atcmp.nl>, by leo@atcmp.nl (Leo Willems): > In the following program a class object its address is put in to a void pointer. > After restoring this pointer into a pointer of the base class, typeinfo seems to > be restored: This will not necessarily be the case, and definitely not with multiple inheritence. When a derived object is being passed around as a pointer to a one of its base classes, that pointer's value is not necessarily == to the address of the derived object. If you take the base*, cast it to a void*, then cast it back to a derived*, you will definitly have problems ( I have been bitten by this :-). Ed Schiebel AT&T Bell Laboratories dog@vilya.att.com