Skip to main content
GameDev.net gamedev.net
🔒 Locked

[SOLVED] C++ Multiple inheritance problem

Started by chema Mar 28, 2007 at 6:15 AM 5 replies 1.3k views
Original Post
chema
chema
Hi, I have a set of interfaces (pure virtual methods) with an inheritance relationship:

                  +--------------+
                  | IBaseTexture |
                  +--------------+
                        ^
                        |
         +--------------+-------------------+
         |              |                   |     
+--------------+ +---------------+ +----------------+
|   ITexture   | |  ICubeTexture | | IVolumeTexture |
+--------------+ +---------------+ +----------------+
I want to write implementation classes for ITexture, ICubeTexture and IVolumeTexture so I added:

                  +--------------+
                  | IBaseTexture |
                  +--------------+
                        ^
                        |
         +--------------+-------------------+
         |              |                   |     
+--------------+ +---------------+ +----------------+
|   ITexture   | |  ICubeTexture | | IVolumeTexture |
+--------------+ +---------------+ +----------------+
        ^               ^                   ^
        |               |                   |
+--------------+ +---------------+ +-----------------+
|  MyTexture   | | MyCubeTexture | | MyVolumeTexture |
+--------------+ +---------------+ +-----------------+
But I want to make a common base interface for MyTexture, MyCubeTexture and MyVolumeTexture. So I added the IMyBaseTexture interface.

                  +--------------+                      +------------------+   
                  | IBaseTexture |<---------------------|  IMyBaseTexture  |   
                  +--------------+                      +------------------+
                        ^                               +    draw() = 0    |
                        |                               +------------------+
         +--------------+-------------------+                  ^
         |              |                   |                  |
+--------------+ +---------------+ +----------------+          |
|   ITexture   | |  ICubeTexture | | IVolumeTexture |          |
+--------------+ +---------------+ +----------------+          |
        ^               ^                   ^                  |
        |  +------------|---+---------------|---+--------------+
        |  |            |   |               |   |
+--------------+ +---------------+ +-----------------+
|   MyTexture  | | MyCubeTexture | | MyVolumeTexture |
+--------------+ +---------------+ +-----------------+
| draw()       | |  draw()       | |  draw()         |
+--------------+ +---------------+ +-----------------+
The problem is: In some point of my program I need to cast a IBaseTexture pointer to a IMyBaseTexture pointer.

void draw(IBaseTexture *i_base_texture) {
   BaseTexture *base_texture = dynamic_cast<BaseTexture *>(i_base_texture);
   base_texture->draw();
}




MSVC++ raises a runtime exception. I have read something about...This kind of situation is called the "dreaded diamond". I have read that using virtual inheritance from IBaseTexture will help... But I cannot change the code of ITexture, ICubeTexture and IVolumeTexture. Any kind of help, even involving paranormal powers, will be appreciated. Thanks, Chema [Edited by - chema on March 29, 2007 8:32:14 AM]
///////////////////////////// Good luck, // // you will need it! /////////////////////////////Chema
Deyja
Deyja
If you can't make those three intermediate interfaces use virtual inheritance, you might be blocked out of the easy fix. I'm not sure, but it seems logical that, if one base is virtual, it will share the non-virtual base just as if they had both been declared virtual. So you can try making the inheritance you can change virtual, and see if it works. In the case that it does, a simple dynamic_cast could perform the casting.

I can't think of a way otherwise that doesn't involve modifying the source of those interfaces.
ToohrVyk
ToohrVyk
You cannot define your class hierarchy through inheritance from the provided class hierarchy, because the root class is not virtual.

I would suggest that you define your class hierarchy from the provided hierarchy by using composition, instead.
Antheus
Antheus
Why isn't draw() part of IBaseTexture?

It is implemented by all descendants, it is part of texture, it performs a function common to all textures.

Muhammad Haggag
Muhammad Haggag
Quote:
Original post by Antheus
Why isn't draw() part of IBaseTexture?

It is implemented by all descendants, it is part of texture, it performs a function common to all textures.

Quoted for truth.

On another note, I'm thoroughly impressed by your unmatched ability to create UML class diagrams in ASCII. You have the patience of a...ninja?

Bregma
Bregma
Quote:
Original post by Antheus
Why isn't draw() part of IBaseTexture?


To futher that train of thought, why is IMyBaseTexture a subclass of IBaseTexture? If it just adds additional functions, make it a mix-in class instead. That's the way multiple inheritance works in the simplicated languages like Java or C#.

Usually when you start banging your head against this sort of wall, it's a sign that you have the wrong design to start with. No amount of Klever Kode Tricks (tm) can save a bad design. No matter how many times managers insist it's true.

--smw
Stephen M. Webb
Professional Free Software Developer
chema
chema
Hello

I have found the problem, I haven't enabled RTTI in my project configuration [rolleyes], that's why dynamic cast didn't work. As Deyja suggested I have changed BaseTexture inheritance from IBaseTexture to a virtual inheritance.

And why isn't draw() part of IBaseTexture? Well this is really the problem, I cannot modify IBaseTexture also (i forgot to say this [oh]) and I wanted to add some methods.

Following your suggestions I have some interested readings about class composition and ascii UML :P

Thank you everybody,

Chema




///////////////////////////// Good luck, // // you will need it! /////////////////////////////Chema

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.