Archived

This topic is now archived and is closed to further replies.

Lestat3D

Multiple inheritance problems

Recommended Posts

Lestat3D    122
What happens if I make a class that inherits from two classes in the same way and those two classes have variables and/or functions with the same name and the same data tyipe and/or the same parameter list?

Share this post


Link to post
Share on other sites
Countach    122
Then you''d get ambiguity. That means the derived class will need to be explicitly cast every time an ambiguous function is called.

Basically its a good idea to avoid this



Reach out and torch someone.

Share this post


Link to post
Share on other sites
Null and Void    1088
Here''s an example of what Countach was talking about:
  
class Base1 {
protected:
int a;
};
class Base2 {
protected:
int a;
};
class Deri : public Base1, public Base2 {
public:
void SetA1(int pA) {
Base1::a = pA;
}
void SetA2(int pA) {
Base2::a = pA;
}
};

Yeah, it''s pretty annoying, try to avoid it.

[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Multiple inheritance is evil!

Interestingly, Ruby, a full-featured OO scripting language, does not permit multiple inheritance for this reason. Instead, it allows for the creation of modules - collections of properties and methods - that can be mixin''ed with any class.

Share this post


Link to post
Share on other sites
Pactuul    122
Multiple inheritance isn''t evil, it''s just not recommended for most applications.

when you have
class Base1 and class Base2

the class Derived will have all the members and like Null and Void said you would just specify which class your referring too by calling instance_of_derived.Base1::Function();

Also to avoid alot of ambiutity (spelling?) you would make the functions that are common to both bases a virtual function. So then it doesn''t matter which one you call.

Pactuul

Share this post


Link to post
Share on other sites
Arild Fines    968
How many OO languages have MI anyway? I can think of only two, namely C++ and Eiffel(or so I have heard). The original OO language, Simula, did not have MI, and neither did Smalltalk. Most newer languages seem to either use the interface paradigm(Java, C#), or are dynamically typed(Python).

"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours." - - Stephen Roberts

Share this post


Link to post
Share on other sites
Lowas    122
C++ ownz, why? Because we have a lot of freedom in the language
and we can choose if we want multiple inheritance or not.

And it isn''t evil at all, that''s just a plain silly statement.
Bad design is evil, bad lead programmers and project leaders are evil.

If the code calls for MI why not use it? ...one should be aware of the pitfalls, but that goes for everything in programming doesn''t it?

The funny thing is that the standard C++ library uses MI itself so we are all using MI to some extent in C++.

Share this post


Link to post
Share on other sites
Oluseyi    2110
I always wondered what happened when you inherited (virtually) from two base classes that both had a property with the same name, but different types... I guess you''ll still need to fully specify which property to use.

Ah, well, back to work.



I wanna work for Microsoft!

Share this post


Link to post
Share on other sites