<i id='OvNGk'><tr id='OvNGk'><dt id='OvNGk'><q id='OvNGk'><span id='OvNGk'><b id='OvNGk'><form id='OvNGk'><ins id='OvNGk'></ins><ul id='OvNGk'></ul><sub id='OvNGk'></sub></form><legend id='OvNGk'></legend><bdo id='OvNGk'><pre id='OvNGk'><center id='OvNGk'></center></pre></bdo></b><th id='OvNGk'></th></span></q></dt></tr></i><div id='OvNGk'><tfoot id='OvNGk'></tfoot><dl id='OvNGk'><fieldset id='OvNGk'></fieldset></dl></div>

    1. <tfoot id='OvNGk'></tfoot>
        <bdo id='OvNGk'></bdo><ul id='OvNGk'></ul>

      <small id='OvNGk'></small><noframes id='OvNGk'>

        <legend id='OvNGk'><style id='OvNGk'><dir id='OvNGk'><q id='OvNGk'></q></dir></style></legend>

        访问另一个子类中基类的受保护成员

        时间:2023-08-02

          <tbody id='zunmC'></tbody>

          <small id='zunmC'></small><noframes id='zunmC'>

          <legend id='zunmC'><style id='zunmC'><dir id='zunmC'><q id='zunmC'></q></dir></style></legend>
          <i id='zunmC'><tr id='zunmC'><dt id='zunmC'><q id='zunmC'><span id='zunmC'><b id='zunmC'><form id='zunmC'><ins id='zunmC'></ins><ul id='zunmC'></ul><sub id='zunmC'></sub></form><legend id='zunmC'></legend><bdo id='zunmC'><pre id='zunmC'><center id='zunmC'></center></pre></bdo></b><th id='zunmC'></th></span></q></dt></tr></i><div id='zunmC'><tfoot id='zunmC'></tfoot><dl id='zunmC'><fieldset id='zunmC'></fieldset></dl></div>
            • <tfoot id='zunmC'></tfoot>
                • <bdo id='zunmC'></bdo><ul id='zunmC'></ul>
                  本文介绍了访问另一个子类中基类的受保护成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  为什么要编译:

                  class FooBase
                  {
                  protected:
                      void fooBase(void);
                  };
                  
                  class Foo : public FooBase
                  {
                  public:
                      void foo(Foo& fooBar)
                      {
                          fooBar.fooBase();
                      }
                  };
                  

                  但这不是吗?

                  class FooBase
                  {
                  protected:
                      void fooBase(void);
                  };
                  
                  class Foo : public FooBase
                  {
                  public:
                      void foo(FooBase& fooBar)
                      {
                          fooBar.fooBase();
                      }
                  };
                  

                  一方面,C++ 授予对该类的所有实例的私有/受保护成员的访问权限,但另一方面,它不授予对子类的所有实例的基类的受保护成员的访问权限.这在我看来相当不一致.

                  On the one hand C++ grants access to private/protected members for all instances of that class, but on the other hand it does not grant access to protected members of a base class for all instances of a subclass. This looks rather inconsistent to me.

                  我已经测试了使用 VC++ 和 ideone.com 进行编译,并且都编译了第一个代码片段,但没有编译第二个代码片段.

                  I have tested compiling with VC++ and with ideone.com and both compile the first but not the second code snippet.

                  推荐答案

                  foo 收到一个 FooBase 引用时,编译器不知道该参数是否是后代Foo,所以它必须假设它不是.Foo 可以访问其他Foo 对象的继承保护成员,而不是所有其他同级类.

                  When foo receives a FooBase reference, the compiler doesn't know whether the argument is a descendant of Foo, so it has to assume it's not. Foo has access to inherited protected members of other Foo objects, not all other sibling classes.

                  考虑这个代码:

                  class FooSibling: public FooBase { };
                  
                  FooSibling sib;
                  Foo f;
                  f.foo(sib); // calls sib.fooBase()!?
                  

                  如果Foo::foo可以调用任意FooBase后代的protected成员,那么它就可以调用FooSibling的protected方法,它有与 Foo 没有直接关系.这不是受保护访问的工作方式.

                  If Foo::foo can call protected members of arbitrary FooBase descendants, then it can call the protected method of FooSibling, which has no direct relationship to Foo. That's not how protected access is supposed to work.

                  如果 Foo 需要访问所有 FooBase 对象的受保护成员,而不仅仅是那些也已知是 Foo 后代的成员,那么 Foo 需要是 FooBase 的朋友:

                  If Foo needs access to protected members of all FooBase objects, not just those that are also known to be Foo descendants, then Foo needs to be a friend of FooBase:

                  class FooBase
                  {
                  protected:
                    void fooBase(void);
                    friend class Foo;
                  };
                  

                  这篇关于访问另一个子类中基类的受保护成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:operator= 和 C++ 中未继承的函数? 下一篇:C++ 对 vtable 和继承的未定义引用

                  相关文章

                  最新文章

                • <i id='Z3utS'><tr id='Z3utS'><dt id='Z3utS'><q id='Z3utS'><span id='Z3utS'><b id='Z3utS'><form id='Z3utS'><ins id='Z3utS'></ins><ul id='Z3utS'></ul><sub id='Z3utS'></sub></form><legend id='Z3utS'></legend><bdo id='Z3utS'><pre id='Z3utS'><center id='Z3utS'></center></pre></bdo></b><th id='Z3utS'></th></span></q></dt></tr></i><div id='Z3utS'><tfoot id='Z3utS'></tfoot><dl id='Z3utS'><fieldset id='Z3utS'></fieldset></dl></div>
                    <bdo id='Z3utS'></bdo><ul id='Z3utS'></ul>
                  <tfoot id='Z3utS'></tfoot>
                • <small id='Z3utS'></small><noframes id='Z3utS'>

                    <legend id='Z3utS'><style id='Z3utS'><dir id='Z3utS'><q id='Z3utS'></q></dir></style></legend>