Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't the C++ compiler disambiguate between an inherited public and an inherited private method with the same name?

I'm confused as to why the C++ compiler won't accept this:

  class Foo { 
    private: void Baz() { }
  };

  class Bar {
    public: void Baz() { 
  };

  class FooBar : public Foo, public Bar { };

  void main() {
    FooBar fb;
    fb.Baz();
  }

The error gcc gives is:

 request for member ‘Baz’ is ambiguous
 candidates are: void Bar::Baz()
                 void Foo::Baz()

but isn't it obvious that I want Bar::Baz(), since Foo::Baz() is private? Why won't the compiler disambiguate here?

like image 546
Justin L. Avatar asked Aug 26 '11 19:08

Justin L.


1 Answers

Name resolution works in two stages. First the name is looked up then the name is checked for access. If the name look up is ambiguous then the access is never considered.

As to why, maybe it's a deliberate language design, but I think more likely it's just to simplify the process of resolving names. The rules are fiendishly complicated already.

like image 187
john Avatar answered Sep 21 '22 06:09

john