![]() |
| ||
| Multiple Inheritance: Ambiguous Function Hey guys, I was wondering, with this code: main.cpp #include <iostream> MinGW's GCC gives me these errors: D:\MyDocs\Code\InheritThis\main.cpp||In function `int main()':| How come the compiler still complains about ambiguity? It knows the function is not overloaded (?) It knows the function is not virtual. Can't the compiler be sure that when I call the function like that, it's the same function? Thanks in advance, PS: I know the solution to this is to use virtual inheritance, but I was wondering why C++ works like it does. |
| ||
| Re: Multiple Inheritance: Ambiguous Function Replying to myself yeey. Let a class in memory be resembled by: [classname] Class inheritance in memory looks like this, according to wiki. class a; class b : a; class a looks like this: [a] b looks like this: [a][b] So if we'd have a class base and c, we can create a diamond inheritance: class base; class a : base; class b : base; class c : a, b; Their memory-pictures are like this: base: [base] a: [base][a] b: [base][b] c: [base][a][base][b][c] c, more specific, looks like this: [a::base][a][b::base][b][c] So, naturally, a::base and b::base data members can differ, but a::base::func() must be the same as b::base::func() given that a and b don't overload and func() is not virtual right? How else can they be different? |
| ||
| Re: Multiple Inheritance: Ambiguous Function > How else can they be different? Yep, they must be the same function. It works if you pick a specific copy like this: mycar.racer::cruise(50);or this mycar.tank::cruise(50); But "diamond inheritance" looks like something to avoid in general. Seems strange that the language wasn't designed to include only one copy in such a case. |
| ||
| Re: Multiple Inheritance: Ambiguous Function Quoting myself: I know the solution to this is to use virtual inheritance, but I was wondering why C++ works like it does. The correct solution is to virtual inherit the class cars in race and tank class. This way, the compiler creates only one car inside of racetank, and 2 vtables to be able to still call the functions. Basically, I was just wondering why GCC doesn't just pick one of the functions and give me a warning instead of 2 errors. --Edit, a bit later: So the compiler asks me what this pointer to give to the function, that of class a or b? It doesn't matter if one only uses class c, but when one degrades class c to class a, b or base, the data members may have different values then what was expected if the compiler chose instead of the programmer. Say function modifies int value, how would the programmer know what value it was modified, that of a or b? Therefore a name must be presented if one wants to call functions like this. The "proper" solution, in most cases, is to go by virtual inheritance. This was helpful research. Multiple Virtual Inheritance seems a nice and powerful tool, albeit a bit tricky. |
| ||
| Re: Multiple Inheritance: Ambiguous Function Quote:
error C2385: 'racetank::car' is ambiguous So it's not a compiler bug or compiler specific thing , it should be something that clearly defined on the C++ language specification. Let this thread open may be later some expertise see this thread and reply. Quote:
yes there is a correct solution for this ,, see this , http://msdn.microsoft.com/en-us/libr...2f(VS.71).aspx anyway why this happen is not still clear. so please keep this thread open. |
| ||
| Re: Multiple Inheritance: Ambiguous Function #include <iostream>This code compiles nicely , so this is the solution , but the problem is still unclear. |
| ||
| Re: Multiple Inheritance: Ambiguous Function No, that's not the proper solution to this. This is #include <iostream> This way, racetank only has one base object car, not two. Virtual Inheritance :D |
| ||
| Re: Multiple Inheritance: Ambiguous Function .... |
| All times are GMT -4. The time now is 7:54 am. |
Forum system based on vBulletin Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
©2003 - 2010 DaniWeb® LLC