Thread: Inheritence ambiguity

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    Inheritence ambiguity

    I have a base class A, which is publicly inherited by both B and C. D is a class that public inherits B and C. There is some ambiguity when I make a certain function call.
    Code:
    #include <iostream>
    using namespace std;
    
    class A
    {
    private:
        int Aint;
    public:
        A():Aint(1) {}
        ~A() {}
        virtual int GetAint(void) {cout << "A::GetAint()" << endl; return Aint; }
    };
    
    class B : public A
    {
    private:
        int Bint;
    public:
        B():Bint(2) {}
        ~B() {}
        virtual int GetBint(void) {return Bint; }
        virtual int GetAint(void) {cout << "B::GetAint()" << endl; return A::GetAint(); }
    };
    
    class C : public A
    {
    private:
        int Cint;
    public:
        C():Cint(3) {}
        ~C() {}
        virtual int GetCint(void) {return Cint; }
        virtual int GetAint(void) {cout << "C::GetAint()" << endl; return A::GetAint(); }
    };
    
    class D : public B, public C
    {
    private:
        int Dint;
    public:
        D():Dint(4) {}
        ~D() {}
        virtual int GetDint(void) {return Dint; }
    };
    
    int main(void)
    {
        D d;
        int temp;
    
        temp=d.GetAint();
        cout << temp << endl;
    
        return 0;
    }
    The call 'd.GetAint()' is ambiguous, and rightly so, because that was the plan all along. My question is, what is the work around here? Can I specify in main() to call a particular inherited function, something like:
    Code:
    temp=d.C:GetAint();
    I know that's not correct but is this possible?

    I am also aware that I could just place a function in D and call whichever one I wanted.
    Last edited by bennyandthejets; 12-04-2003 at 05:09 PM.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Not sure if its really correct, but this does seem to work...

    Code:
    temp=d.B::GetAint();
    When I change to that, my output is like this:

    B::GetAint()
    A::GetAint()
    1
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Ha! I didn't think it was so easy, so I didn't even try that. Thanks. Now for the crunch question: find me a real life application of classes that would result in this kind of ambiguity.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What about this:
    Code:
    temp=d.B.A::GetAint();
        cout << temp << endl;
    Using the same classes as before. Seeing as A::GetAint() is publicly inherited throughout, it should be possible to access it from an instance of D. What is the syntax for this?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why no ambiguity error in code?
    By George2 in forum C++ Programming
    Replies: 5
    Last Post: 02-27-2008, 08:06 AM
  2. Ambiguity issue
    By George2 in forum C++ Programming
    Replies: 21
    Last Post: 02-11-2008, 12:52 AM
  3. Ambiguity error with double as parameter type
    By tejasvsrinivasa in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2007, 04:17 PM
  4. class inheritence with constructor parameters
    By drdodirty2002 in forum C++ Programming
    Replies: 3
    Last Post: 06-20-2005, 06:49 PM
  5. Operator overloading and inheritence
    By ventolin in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2004, 11:35 AM