🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Joel Spolsky Tue, 18 Dec 1990 06:50
help! with multiple inheritance
#354
Author: Joel Spolsky
Date: Tue, 18 Dec 1990 06:50
61 lines
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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads