Article View: gnu.g++.help
Article #354help! with multiple inheritance
From: Joel Spolsky
Date: Tue, 18 Dec 1990 06:50
Date: Tue, 18 Dec 1990 06:50
61 lines
2021 bytes
2021 bytes
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
Technical Details
Message-ID:
<27840@cs.yale.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!yale!quasi-eli!cs.yale.edu!spolsky-joelShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!yale!quasi-eli!cs.yale.edu!spolsky-joel From: spolsky-joel@CS.YALE.EDU (Joel Spolsky) Newsgroups: gnu.g++.help,comp.lang.c++ Subject: help! with multiple inheritance Message-ID: <27840@cs.yale.edu> Date: 18 Dec 90 06:50:28 GMT Sender: news@cs.yale.edu Followup-To: gnu.g++.help Organization: Yale University Computer Science Dept., New Haven, CT 06520-2158 Lines: 61 Nntp-Posting-Host: zoo-gw.cs.yale.edu