Multiple Inheritance Bad?

Started by
13 comments, last by Digital Extent 21 years, 3 months ago
I don''t see why you want to use multiple inheritance here. The D3DSprite will inherit from sprite and...?

Don''t listen to me. I''ve had too much coffee.
Advertisement
It mainly just isn''t all that useful. Many of the "has a" relationships would properly be "is a" relationships, but actually making it an "is a" is a burden. You cannot inherit off of a class of classes. You inheret off a specific class. With "has a" you can use a polymorphic base class and at run time use one of the classes derived off of it. Several template techniques use multiple inheritance. It isn''t nearly the burden there. It is still done at compile time though.

Under extreme conditions you could have an object model where a class inherits off of three classes where each class is one of a set of ten classes. That makes a thousand possible derived classes. If you actually need to use all of those at run time then you have to select with of the thousand to actually instantiate. Frigging nightmare. It is far easier to use three conditions of ten cases than one conditional of a thousand. So in many cases where multiple-inheritance would be conceptually correct it just flat out isn''t practical. It is easier to get a contrived "has a" relationship to work correctly than to use multiple inheritance even though things fall more naturally into place using it. Most people miss the elegance of a conditional running ten functions deep

If C++ was an interpretted language then maybe you could specify the bases to inherit off of at run time. If so then you would most likely see multiple inheritance used extensively. You would most likely hear people arguing that not using multiple inheritance needlessly complicates things. You got to write all this extra code for all these special cases. You have to create new classes just to get it to work. That isn''t the case though. As it is using multiple inheritance is such a burden that getting "has a" to work correctly is by no means a needless complication.
Keys to success: Ability, ambition and opportunity.
I have used both MI and Virtual MI in games and had almost no problems with them. Many times I will implement simple classes with MI and find that composition works better. Changing from MI to composition is EASY! Just find the places where you called the inherited functions and used inherited data, and put "m_pObjectName->" or "m_objectName." in front of those statements. Works without a single complaint.

Exception: If you actually use polymorphism on that specific class and the compiler is expecting to be able to call functions polymophically, you might be in trouble. If you are just calling the functions directly, you can stick wrapper functions to emulate the inheritance you relied upon.

Example:

class Base1
{
public:
int Foo(void);
};

class Base2{

public:
int Bar(void);
};

class Derived : public Base1, public Base2
{
public:
// anything else
};



Well, this can easily become:

class Derived : public Base1
{
public:
int Bar(void) { return m_base2->Bar(); }
private:
Base2 m_base2;
};


As far as programming problems go, this really isn't that bad.

[edited by - Rick Scott on December 26, 2002 6:49:04 PM]
That is the difference between "has a" and "is a" relationship.

The situation is:

class Sprite
{
//..
};

class Direct3DDependantObject
{
//.
};

Now then, D3DSprite is a Sprite, and it "is" also a Direct3DDependantObject is it not?
Programming is easy, thinking is hard.
The use of the term ''DependantObject'' shows that you''re trying to change a ''depends on'' relationship into an ''is a'' relationship. It would be like deriving ''Man'' from a ''PotentiallyMarriedToWoman'' class. You don''t do that, you have a ''MarriedTo'' member. D3DSprite can be different from Sprite in its choice and use of members. It doesn''t need to derive from another class. I don''t see a compelling reason for a ''Direct3DDependantObject'' to exist.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

This topic is closed to new replies.

Advertisement