重載、重寫和隱藏

來源:互聯網
上載者:User

標籤:style   color   sp   c   bs   as   r   不同的   函數   

重載:

   a 相同的範圍(在同一個類中)

   b 函數名字相同

   c 參數不同(傳回型別不足以區分重載函數)

   d virtual關鍵字可有可無

重寫:(實際上就是虛函數動態綁定)

   a 不同的範圍,分別位於基類和衍生類別中

   b 函數的名字相同

   c 參數相同

   e 一般情況下傳回型別相同(除非傳回型別分別為基類指標和衍生類別指標)

   d 基類函數必須有virtual關鍵字

隱藏:

   a 如果衍生類別的函數和基類的函數同名,但是參數不同,此時,不管有無virtual,基類的函數被隱藏。

   b 如果衍生類別的函數與基類的函數同名,並且參數也相同,但是基類函數沒有vitual關鍵字,此時,基類的函數

   要避免隱藏,例如

   class Base

   {

    public:

        void func(int);

   }

   class Derived:public Base

   {

    public:

        //using Base::func;

        void func();

   }

   int main()

   {

       Derived d;

       d.func()//ok,Derived::func()

       d.func(1)//error,Base::func(int)被隱藏

   }

為了讓d.func(1)正確,需要在Derived的定義中加入using Base::func;

重載、重寫和隱藏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.