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

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

      1. 在 C++ 中覆盖 Base 的重载函数

        时间:2023-08-02
        <legend id='w9oE3'><style id='w9oE3'><dir id='w9oE3'><q id='w9oE3'></q></dir></style></legend>
          <tbody id='w9oE3'></tbody>

              • <bdo id='w9oE3'></bdo><ul id='w9oE3'></ul>

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

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

                2. 本文介绍了在 C++ 中覆盖 Base 的重载函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  可能的重复:
                  C++ 重载解析

                  我遇到了一个问题,在我的类覆盖其基类的函数后,所有重载的函数版本都被隐藏了.这是设计使然还是我做错了什么?

                  I ran into a problem where after my class overrode a function of its base class, all of the overloaded versions of the functions were then hidden. Is this by design or am I just doing something wrong?

                  例如

                  class foo
                  {
                    public:
                      foo(void);
                      ~foo(void);
                      virtual void a(int);
                      virtual void a(double);
                  };
                  
                  class bar : public foo 
                  {
                    public:
                      bar(void);
                      ~bar(void);
                      void a(int);
                  };
                  

                  下面会给出一个编译错误,说明 bar 中没有 a(double) 函数.

                  the following would then give a compile error saying there is no a(double) function in bar.

                  main() 
                  {
                    double i = 0.0;
                    bar b;
                    b.a(i);
                  }
                  

                  推荐答案

                  在类栏中,添加

                  using foo::a;
                  

                  这是 C++ 中常见的陷阱".一旦在 a 类作用域中找到名称匹配,它就不会在继承树中进一步查找重载.通过指定 'using' 声明,您将 'a' 的所有重载从 'foo' 带入了 'bar' 的范围.然后重载正常工作.

                  This is a common 'gotcha' in C++. Once a name match is found in the a class scope, it doesn't look further up the inheritance tree for overloads. By specifying the 'using' declaration, you bring all of the overloads of 'a' from 'foo' into the scope of 'bar'. Then overloading works properly.

                  请记住,如果存在使用 'foo' 类的现有代码,其含义可能会因额外的重载而改变.或者额外的重载可能会引入歧义,并且代码将无法编译.詹姆斯·霍普金 (James Hopkin) 的回答中指出了这一点.

                  Keep in mind that if there is existing code using the 'foo' class, its meaning could be changed by the additional overloads. Or the additional overloads could introduce ambiguity and and the code will fail to compile. This is pointed out in James Hopkin's answer.

                  这篇关于在 C++ 中覆盖 Base 的重载函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:防止 C++ 中的类继承 下一篇:使用初始化列表(C++)初始化父级的受保护成员

                  相关文章

                  最新文章

                    <bdo id='1I12z'></bdo><ul id='1I12z'></ul>
                3. <legend id='1I12z'><style id='1I12z'><dir id='1I12z'><q id='1I12z'></q></dir></style></legend>

                      <tfoot id='1I12z'></tfoot>

                      <small id='1I12z'></small><noframes id='1I12z'>

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