Author Topic: Weird confusion with multiple inheritance  (Read 4319 times)

0 Members and 1 Guest are viewing this topic.

pllaybuoy

  • Guest
Weird confusion with multiple inheritance
« on: January 25, 2013, 08:29:01 pm »
Code: [Select]
//base class of all oh holy lawrd of shiny tit
class character{
private:
int level;
public:
character();//constructor
};

two classes derived from the base
class allies:public character
{
public:
allies():character(){};//constructor of first derived class
};
class enemy:public character{
public:
enemy():character(){};//constructor of first derived class
};

//derived from derived classes
class warrior:public allies{
public:
warrior():allies(){};//constructor
};

class demon:public enemy{
public:
demon():enemy(){};//constructor
};

//the real problem
class spy:public allies,public enemy
{
public:
spy();
};





So as you all can see there is a yo dawg situation or derivation of derivation with one being multiple inheritance .
here's what confuses me , the spy class is inherited both from enemy and allies as shown above now when you create an object of spy it will call the allies constructor and the enemy constructor , cool right ?
but allies will call the character constructor and enemy will call the character constructor too since character is the base of both allies and enemy so for one object we have two instances of the class character created , burden on memory or okay ? I admit this is a shitty example but consider the situtation in some hard core programming project where you gotta make the sleekest and fastest app .One thing more , so considering the above example there will be two 'level' variables since there will be two character objects which one the object of class spy use ? I know this is some really retarded situation that came in my mind , but well there is a possibilty of this being true , what should one do in this situation ? its like a dead end

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Weird confusion with multiple inheritance
« Reply #1 on: January 29, 2013, 10:35:34 am »
The example which is proposed is known to some as the 'Diamond of Doom'.  Generally this is considered a simple design flaw.  It can be corrected different ways but generally if you have something like this, you're simply doing it wrong.  Multiple-inheritance can be very useful IF you need it but it's not usually a requirement.  In this case, the 'spy' class would be better redesigned.  It may be best to, instead, have it contain instances of 'ally' and 'enemy'.  Since it is still a 'character' of some sort, you can still inherit from that class and have member instances for the other classes to portray the different identities that the spy might have.  This could even provide some added flexibility if you want to retain a list of different identities on either side that the spy might possess.


A very decent explanation can be found on StackOverflow: http://stackoverflow.com/questions/406081/why-should-i-avoid-multiple-inheritance-in-c


If you require additional explanation/elaboration, just ask.


I do apologize for the long wait for the response; I have been quite busy with work as of late.
« Last Edit: January 30, 2013, 11:17:55 am by Xires »
-Xires