Thread: Multiple Inheritance Ambiguity

  1. #1
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Multiple Inheritance Ambiguity

    example in question:
    Code:
    class Base1
       {
    public:
       void func();
       };
    
    class Base2
       {
    public:
       void func();
       };
    
    class Derived : public Base1,
                    public Base2
       {
       };
    1- in the example above, how is ambiguity resolved
    2- what if one or more of them were virtual
    3- what other evils might multiple inheritance cause that I haven't thought of?
    Last edited by FillYourBrain; 08-23-2002 at 08:26 AM.

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    1. What ambiguity?
    2. If the bases are virtual than you inherit the public implementations of them in the derived class.
    3. Protected and Private iheritance is tricky because you iherit not just the public implimentations.

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    there are two functions of the same name. both classes are inherited. THAT ambiguity. I'll change the funcs to public. That shouldn't matter anyway.

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Sorry, virtual means that you iherit the public interface, not the implementation. Non virtual is the implimentation. Think in terms of implementations and interfaces.

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Than it probably has to be preceded by it's scope identifier, but I can't say for sure, being a Cer rather than a ligitimate C++er.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    dude, you're not addressing the question. It's about multiple inheritance and the ambiguity introduced. I'm well aware of what virtual does normally.

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Try using a scope identifier:

    Derived Obj;
    Obj.Base1::func(...);

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    The question is what if you don't specify which function. Which one does it call. That's where the term "ambiguity" comes in.

  9. #9

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Hopefully it provides a helpful error message, but with VC++6 there is no telling which one it will use.

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    but I'm sure there is a standard

  12. #12
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    There is a book called C++ FAQ. Maybe it's in there, or the C++ programming language. Why don't you try testing it.

  13. #13
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I found the answer.

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <iostream>
    using namespace std;
    
    class foo{
    public:
    	virtual void Sing(){cout << "Foo lalala" << endl;}
    	
    };
    class bar{
    public:
    	virtual void Sing(){cout << "Bar lalala" << endl;}
    
    };
    class foobar: public foo,public bar{
    public:
    /*Can do what's below...but then why inherit?!..*/
    //	void Sing(){foo::Sing();bar::Sing();}
    };
    
    int main(void) {
    	foobar fb;
    	foo* ptrfoo = &fb;
    	bar* ptrbar = &fb;
    	
    //	fb.Sing(); //Will cause the ambiguity error!!
    	fb.foo::Sing();
    	fb.bar::Sing();
    
    	ptrfoo->Sing();
    	ptrbar->Sing();
    
    	return 0;
    }
    I dont use much MI...but here's a few ideas...

    You can implement a Sing() func in the derived class to call the other functions through the scope operator..not great unless you wish to call both....

    You can also call the member function from an instance by specifying the base class

    Also you can do it by calling from a casting up with a base pointer....

  15. #15
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    In other words, do exactly what I said above.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. references and multiple inheritance - incompatible?
    By m37h0d in forum C++ Programming
    Replies: 11
    Last Post: 09-03-2008, 02:35 PM
  3. Multiple inheritance in C#
    By DavidP in forum C# Programming
    Replies: 1
    Last Post: 06-27-2008, 04:41 PM
  4. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  5. Multiple virtual inheritance
    By kitten in forum C++ Programming
    Replies: 3
    Last Post: 08-10-2001, 10:04 PM