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

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

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

      C++ 继承和函数覆盖

      时间:2023-08-02

        <bdo id='gjE9P'></bdo><ul id='gjE9P'></ul>

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

          <legend id='gjE9P'><style id='gjE9P'><dir id='gjE9P'><q id='gjE9P'></q></dir></style></legend>
          • <tfoot id='gjE9P'></tfoot>
              <tbody id='gjE9P'></tbody>
                <i id='gjE9P'><tr id='gjE9P'><dt id='gjE9P'><q id='gjE9P'><span id='gjE9P'><b id='gjE9P'><form id='gjE9P'><ins id='gjE9P'></ins><ul id='gjE9P'></ul><sub id='gjE9P'></sub></form><legend id='gjE9P'></legend><bdo id='gjE9P'><pre id='gjE9P'><center id='gjE9P'></center></pre></bdo></b><th id='gjE9P'></th></span></q></dt></tr></i><div id='gjE9P'><tfoot id='gjE9P'></tfoot><dl id='gjE9P'><fieldset id='gjE9P'></fieldset></dl></div>
              • 本文介绍了C++ 继承和函数覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                在C++中,基类的成员函数是否会被其同名的派生类函数覆盖,即使它的原型(参数的数量、类型和常量)不同?我想这是一个愚蠢的问题,因为许多网站都说函数原型应该是相同的;但是为什么下面的代码不能编译?我相信这是一个非常简单的继承案例.

                In C++, will a member function of a base class be overridden by its derived class function of the same name, even if its prototype (parameters' count, type and constness) is different? I guess this a silly question, since many websites says that the function prototype should be the same for that to happen; but why doesn't the below code compile? It's a very simple case of inheritance, I believe.

                #include <iostream>
                using std::cout;
                using std::endl;
                
                class A {};
                class B {};
                
                class X
                {
                public:
                    void spray(A&)
                    {
                        cout << "Class A" << endl;
                    }
                };
                
                class Y : public X
                {
                public:
                    void spray(B&)
                    {
                        cout << "Class B" << endl;
                    }
                };
                
                int main()
                {
                    A a;
                    B b;
                    Y y;
                
                    y.spray(a);
                    y.spray(b);
                
                    return 0;
                }
                

                GCC 抛出

                error: no matching function for call to `Y::spray(A&)'
                note: candidates are: void Y::spray(B&)
                

                推荐答案

                用来描述这个的术语是隐藏",而不是覆盖".默认情况下,派生类的成员将使具有相同名称的基类的任何成员无法访问,无论它们是否具有相同的签名.如果要访问基类成员,可以使用 using 声明将它们拉入派生类.在这种情况下,将以下内容添加到 class Y:

                The term used to describe this is "hiding", rather than "overriding". A member of a derived class will, by default, make any members of base classes with the same name inaccessible, whether or not they have the same signature. If you want to access the base class members, you can pull them into the derived class with a using declaration. In this case, add the following to class Y:

                using X::spray;
                

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

                上一篇:为什么不从 std::allocator 继承 下一篇:C++中的公共虚函数派生私有

                相关文章

                最新文章

                  <bdo id='8Usmi'></bdo><ul id='8Usmi'></ul>

                <tfoot id='8Usmi'></tfoot>

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

                    <legend id='8Usmi'><style id='8Usmi'><dir id='8Usmi'><q id='8Usmi'></q></dir></style></legend>
                  1. <small id='8Usmi'></small><noframes id='8Usmi'>