<tfoot id='j1ikw'></tfoot>

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

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

      更改派生类中的函数访问模式

      时间:2023-08-02

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

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

            <tbody id='HBpuq'></tbody>

            • <bdo id='HBpuq'></bdo><ul id='HBpuq'></ul>
              1. <tfoot id='HBpuq'></tfoot>

                本文介绍了更改派生类中的函数访问模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                考虑以下片段:

                struct Base
                {
                  virtual ~Base() {}
                
                  virtual void Foo() const = 0; // Public
                };
                
                class Child : public Base
                {
                  virtual void Foo() const {} // Private
                };
                
                int main()
                {
                  Child child;
                
                  child.Foo(); // Won't work. Foo is private in this context.
                
                  static_cast<Base&> (child).Foo(); // Okay. Foo is public in this context.
                }
                

                这是合法的 C++ 吗?this"正在改变派生类中虚函数的访问模式.

                Is this legal C++? "This" being changing the virtual function's access mode in the derived class.

                推荐答案

                是的,在派生类中改变访问模式是合法的.

                Yes, changing the access mode in derived classes is legal.

                这在形式上相似,但在意图上与不同/Non-Virtual_Interface" rel="noreferrer">非虚拟接口 习惯用法.此处给出了一些理由:

                This is similar in form but different in intent to the Non-Virtual Interface idiom. Some rationale is given here:

                关键是存在允许定制的虚函数;除非它们还需要从派生类的代码中直接调用,否则除了私有之外,没有必要将它们设为任何东西.

                The point is that virtual functions exist to allow customization; unless they also need to be invoked directly from within derived classes' code, there's no need to ever make them anything but private.

                至于为什么您实际上会在基础中制作public,而在没有privateprotected 的派生中制作private继承超出了我的范围.

                As to why you would actually make something public in base but private in derived without private or protected inheritance is beyond me.

                这篇关于更改派生类中的函数访问模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:C++ 继承 - 无法访问的基础? 下一篇:operator= 和 C++ 中未继承的函数?

                相关文章

                最新文章

                1. <small id='xufLD'></small><noframes id='xufLD'>

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

                2. <tfoot id='xufLD'></tfoot>
                  <legend id='xufLD'><style id='xufLD'><dir id='xufLD'><q id='xufLD'></q></dir></style></legend>