Multiple inheritance problem

Started by
29 comments, last by Polymorphic OOP 19 years, 7 months ago
Firstly if you reply with a post saying multiple inheritance is bad you will be cursed. I am well aware of the controversy and discussions over using MI. So this is essentially the class structure I've got... A E | | B D \ / C I call new on class C and store the pointers to that call as pointers of class E. This works well enough. However when I want to cast a pointer of class E which was originally created as a class C object to a pointer of class A I cannot do this directly. From what I can tell I need to first cast the class E pointer to class C then to class A. My problem is I won't know exact type of class C until runtime. Would there be a way to cast a pointer of class E to a pointer of class A without knowing the exact type of class C? Hope someone understands what I'm talking about.
Rob Segal - Software Developerrob@sarcasticcoder.com
Advertisement
it sounds like you just messed up your object architecture. if you are using multiple inheritence you should never be needing to cast from one parent type to another without explicitly knowing the type of C. typically you'll have multiple inheritence only so specific sub-systems can concern themselves with the parent class objects, with no need to cross the hierarchy. so my suggestion would be to redesign things so you don't need to ever cast from a->e. if a and e always go together, they should be the same class or at least part of the same linear hierarchy.

otherwise, some bastardized form of dynamic_cast'ing to C. once you have C you automatically have a pointer to A.

-me
There shouldn't be anything wrong, are sure your using the wright cast operator? you need the dynamic_cast operator to navigate type hierarchies, if you cast to a pointer it will indicate if it fails by returning zero, if you cast to a reference and it fails it will throw a bad_cast exception.

Also make sure your destructors are virtual aswell.
Quote:Original post by snk_kid
There shouldn't be anything wrong, are sure your using the wright cast operator? you need the dynamic_cast operator to navigate type hierarchies, if you cast to a pointer it will indicate if it fails by returning zero, if you cast to a reference and it fails it will throw a bad_cast exception.

Also make sure your destructors are virtual aswell.

I thought you had to have RTTI enabled to use dynamic_cast?
Rob Segal - Software Developerrob@sarcasticcoder.com
You can always cast something to something else with old C-style casting.
A* theA = (A*)theEclass;

It's just a horribly unsafe habit to get into. Since you've designed yourself into a corner with the multiple inheritance you may just have to resort to such code thuggery.
Tom Spilman Co-owner | Programmer www.sickheadgames.com
Quote:Original post by pingz
You can always cast something to something else with old C-style casting.

A* theA = (A*)theEclass;


It's just a horribly unsafe habit to get into. Since you've designed yourself into a corner with the multiple inheritance you may just have to resort to such code thuggery.


Hehe code thugery, I like that. Yes I may have to do that. Thanks everyone for all the replies so far.
Rob Segal - Software Developerrob@sarcasticcoder.com
Quote:Original post by rsegal
Quote:Original post by snk_kid
There shouldn't be anything wrong, are sure your using the wright cast operator? you need the dynamic_cast operator to navigate type hierarchies, if you cast to a pointer it will indicate if it fails by returning zero, if you cast to a reference and it fails it will throw a bad_cast exception.

Also make sure your destructors are virtual aswell.

I thought you had to have RTTI enabled to use dynamic_cast?


Yes if your using VC++ compilers, dynamic_cast is specifically for casting polymorphic types, not just between parent-child but also siblings aswell.

Don't use C style casts for this, it was never designed for polymorphic type navigation in mind.

Quote:Original post by rsegal
Quote:Original post by pingz
You can always cast something to something else with old C-style casting.

A* theA = (A*)theEclass;


It's just a horribly unsafe habit to get into. Since you've designed yourself into a corner with the multiple inheritance you may just have to resort to such code thuggery.


Hehe code thugery, I like that. Yes I may have to do that. Thanks everyone for all the replies so far.


There is absolutely no need for "code thugery"
A and E are not related, so there is no rational conversion between them. Where's the problem here?

Redesign.
Quote:Original post by Oluseyi
A and E are not related, so there is no rational conversion between them. Where's the problem here?

Redesign.


Yes the types A & E are not related types but E refers to an instance of type C which is related to A & E, take this example and see:

#include <iostream>struct A { virtual ~A() {} };struct E { virtual ~E() {} };struct B : A {};struct D : E {};struct C : B, D {};int main() {   E* f = new C;   A* g = dynamic_cast<A*>(f);   std::cout << "is null? " << std::boolalpha << (g == 0) << std::endl;   delete g;   return 0;}
Another alternative is to simply store both a pointer to A and E at the time you create C. Presumably you need the pointers for 2 unrelated systems, so at creation time pass C through to those 2 systems instead of trying to pass from 1 system to another (which really is screaming REDESIGN ME!!!).

EDIT: System may not be the best word, but you get the idea....
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V

This topic is closed to new replies.

Advertisement