最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
C++不要在構造函數或析構函數中調用虛函數的原因是什么

這篇文章主要講解了“C++不要在構造函數或析構函數中調用虛函數的原因是什么”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“C++不要在構造函數或析構函數中調用虛函數的原因是什么”吧!

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供阜平網站建設、阜平做網站、阜平網站設計、阜平網站制作等企業(yè)網站建設、網頁設計與制作、阜平企業(yè)網站模板建站服務,十余年阜平做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。


C++不要在構造函數或析構函數中調用虛函數的原因是什么

Reason(原因)

到目前為止,被調用的函數應該只屬于構造對象本身,而不是可能存在于派生類中的某個覆蓋函數。那樣做非常難理解。最壞的情況,在構造函數或者析構函數中直接或間接調用一個沒有實現的純虛函數會導致沒有定義的行為。

Example, bad(反面示例)
 
class Base {
public:
   virtual void f() = 0;   // not implemented
   virtual void g();       // implemented with Base version
   virtual void h();       // implemented with Base version
   virtual ~Base();        // implemented with Base version
};

class Derived : public Base {
public:
   void g() override;   // provide Derived implementation
   void h() final;      // provide Derived implementation

   Derived()
   {
       // BAD: attempt to call an unimplemented virtual function
       f();

       // BAD: will call Derived::g, not dispatch further virtually
       g();

       // GOOD: explicitly state intent to call only the visible version
       Derived::g();

       // ok, no qualification needed, h is final
       h();
   }
};
     

注意:調用一個特定的限定函數不是虛調用,即使這個函數是虛函數。

See also factory functions for how to achieve the effect of a call to a derived class function without risking undefined behavior.

參考工廠函數以便了解如何達成調用派生類功能的效果而不必承擔引起未定義行為的風險。

Note(注意)

There is nothing inherently wrong with calling virtual functions from constructors and destructors. The semantics of such calls is type safe. However, experience shows that such calls are rarely needed, easily confuse maintainers, and become a source of errors when used by novices.

從構造函數和析構函數中調用虛函數并不是本身有什么錯誤。這種調用的語義是安全的。然而,經驗表明這樣的調用很少是必須的,很容易擾亂維護者,如果被新手使用會成為錯誤源。

Enforcement(實施建議)
  • Flag calls of virtual functions from constructors and destructors.

  • 提示來自構造函數或析構函數的虛函數調用。

感謝各位的閱讀,以上就是“C++不要在構造函數或析構函數中調用虛函數的原因是什么”的內容了,經過本文的學習后,相信大家對C++不要在構造函數或析構函數中調用虛函數的原因是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關知識點的文章,歡迎關注!


分享名稱:C++不要在構造函數或析構函數中調用虛函數的原因是什么
文章位置:http://fisionsoft.com.cn/article/gdehgi.html