<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C++核心準則E.13: 直接擁有一個對象所有權時永遠不要拋出異常

          共 1703字,需瀏覽 4分鐘

           ·

          2020-07-31 15:20

          E.13: Never throw while being the direct owner of an object

          E.13: 直接擁有一個對象所有權時永遠不要拋出異常

          Reason(原因)

          That would be a leak.

          那樣做會發(fā)生泄露。


          Example(示例)

          void leak(int x)   // don't: may leak{    auto p = new int{7};    if (x < 0) throw Get_me_out_of_here{};  // may leak *p    // ...    delete p;   // we may never get here}

          One?way of avoiding such problems is to use resource handles consistently:

          避免這種問題的一種方法是始終如一地使用資源句柄。

          void no_leak(int x){    auto p = make_unique(7);    if (x < 0) throw Get_me_out_of_here{};  // will delete *p if necessary    // ...    // no need for delete p}

          Another?solution?(often?better)?would?be?to?use?a?local?variable?to?eliminate?explicit?use?of?pointers:

          另外一個解決方案(通常更好)是用局部變量來避免使用指針。

          void no_leak_simplified(int x){    vector v(7);    // ...}

          Note(注意)

          If you have local "things" that requires cleanup, but is not represented by an object with a destructor, such cleanup must also be done before a?throw. Sometimes,?finally()?can make such unsystematic cleanup a bit more manageable.

          如果局部的“某物”需要清除,但卻沒有實現(xiàn)為一個具有析構函數(shù)的對象,這些清理操作也必須在拋出異常之前進行。有時,finally函數(shù)可以讓這種非系統(tǒng)化的清理動作稍微容易管理一些。


          原文鏈接https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#e13-never-throw-while-being-the-direct-owner-of-an-object

          新書介紹

          以下是本人3月份出版的新書,拜托多多關注!


          本書利用Python 的標準GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設計模式逐個進行說明。這樣一方面可以使讀者了解真實的軟件開發(fā)工作中每個設計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進行說明,讓讀者明白在編寫代碼時如何判斷使用設計模式的利弊,并合理運用設計模式。

          對設計模式感興趣而且希望隨學隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學習Python GUI 編程的讀者可以將本書中的示例作為設計和開發(fā)的參考;使用Python 語言進行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎,迅速構建自己的系統(tǒng)架構。




          覺得本文有幫助?請分享給更多人。

          關注微信公眾號【面向對象思考】輕松學習每一天!

          面向對象開發(fā),面向對象思考!



          瀏覽 38
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  蜜桃视频欧美一区二区 | 青青草久久久 | 亚洲五月天堂 | 很污的视频网站 | 少妇夫妻性生活的视频一级 |