为什么C ++编译器不会在同名的继承的公共方法和继承的私有方法之间进行歧义处理? [英] Why won't the C++ compiler disambiguate between an inherited public and an inherited private method with the same name?

查看:62
本文介绍了为什么C ++编译器不会在同名的继承的公共方法和继承的私有方法之间进行歧义处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++编译器为什么不接受这一点感到困惑:

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();
  }

gcc给出的错误是:

The error gcc gives is:

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

但是由于Foo :: Baz()是私有的,我是否想要Bar :: Baz()并不明显?为什么编译器在这里不会歧义?

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

推荐答案

名称解析分为两个阶段.首先,查询名称,然后检查名称以进行访问.如果名称查找不明确,则永远不会考虑访问权限.

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.

这篇关于为什么C ++编译器不会在同名的继承的公共方法和继承的私有方法之间进行歧义处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆