spolsky-joel@CS.YALE.EDU (Joel Spolsky) (12/18/90)
Hi, Perhaps some kind soul can tell me why this program fragment doesn't work? I am trying to use a base pointer and get the correct derived method, but this doesn't work when the base pointer has been cast to a void * and then back to the base pointer. [Why, you may ask, do I need to cast this through a void pointer? well, because I am using InterViews, and I am using InterViews "Pictures" to store things, and when I get them back they have been cast into Graphic *'s, so I have to cast them back, but by then the derived methods don't work. Arg!] (I'm using g++ version 1.37.1 for Sun SPARC station, Sun OS 4.0) #include "std.h" class a {int x;}; class b {float x,y;}; class Tiger { public: virtual void Roar (void) {printf("roar\n");} }; class Bengal: public a, public Tiger{ public: virtual void Roar (void) {printf("bengal roar\n");} }; class Senegal: public b, public Tiger{ public: virtual void Roar (void) {printf("senegal roar\n");} }; main() { Senegal *ps = new Senegal; // This works, printing "senegal roar": printf("cast to Tiger *:\n"); ((Tiger *) ps)->Roar(); // This segment faults: printf("cast thru:\n"); ((Tiger *) (void *) ps)->Roar(); } Note: There is no problem if Bengal and Senegal do _not_ multiple inherit. Thanks in advance for your help, -- Joel Spolsky // And these streets, Quiet as a sleeping army spolsky@cs.yale.edu // Send their battered dreams to heaven. _Paul Simon