CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2005
    Posts
    8

    Unhappy Ambigous!!! But why???

    If I have

    class Base1 {
    public:
    int myFunc();
    };

    class Base2 {
    public:
    void myFunc();
    };

    class Derived: public Base1, // Derived doesn't declare
    public Base2 { // a function called doIt
    ...
    };
    Derived d;
    d.myFunc(); // error! — ambiguous
    That fine No probs

    but when I have
    class Base1 {
    public:
    int myFunc();
    };

    class Base2 {
    private:
    void myFunc(); // this function is now
    }; // private
    class Derived: public Base1, public Base2
    { ... }; // same as above

    Derived d;
    int i = d.myFunc(); // error! — still ambiguous!

    The call to myFunc() continues to be ambiguous, even though only the function in Base1 is accessible!
    Why do we have such behaviour??

    Thanx for ur response in advance.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Ambigous!!! But why???

    The compiler detects ambiguities by performing tests in this order:
    1. If access to the name is ambiguous, an error message is generated.
    2. If overloaded functions are unambiguous, they are resolved.
    3. If access to the name violates member-access permission, an error message is generated.

    The names are checked before the access permission.

    You can get rid of it by using:
    Code:
    Derived d;
    int i = d.Base1::myFunc();
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Ambigous!!! But why???

    You can also get rid of it using "using"
    Code:
    class Derived : public Base1, public Base2
    {
    public:
       using Base2::myFunc();
    };

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured