<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.19:如果無法選擇適當?shù)馁Y源句柄,使用final_action表現(xiàn)清除處理?

          共 1634字,需瀏覽 4分鐘

           ·

          2020-08-08 20:47

          E.19: Use a?final_action?object to express cleanup if no suitable resource handle is available

          E.19:如果無法選擇適當?shù)馁Y源句柄,使用final_action表現(xiàn)清除處理


          Reason(原因)

          finally?is less verbose and harder to get wrong than?try/catch.

          和try/catch比起來,finally更加簡練并不容易出錯。


          Example(示例)

          void f(int n)
          {
          void* p = malloc(n);
          auto _ = finally([p] { free(p); });
          // ...
          }
          Note(注意)

          finally?is not as messy as?try/catch, but it is still ad-hoc. Prefer?proper resource management objects. Consider?finally?a last resort.

          finally不像try/catch那樣凌亂,然仍然是針對具體問題的特殊對策。使用適當?shù)馁Y源管理對象的方式更好。將finally視為最后一招。


          Note(注意)

          Use of?finally?is a systematic and reasonably clean alternative to the old?goto exit;?technique?for dealing with cleanup where resource management is not systematic.

          使用finally一種系統(tǒng)化、合理化的代替既有代碼中goto exit的方式。使用這個技術可以處理資源沒有被系統(tǒng)化管理的問題。


          Enforcement(實施建議)

          Heuristic: Detect?goto exit;

          啟發(fā)式的:檢出goto exit;


          關于finally

          finally是gsl提供的一個支持函數(shù),可以生成一個用戶釋放資源的清除動作。具體實現(xiàn)請參考以下鏈接:https://github.com/microsoft/GSL/blob/master/include/gsl/gsl_util

          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#e19-use-a-final_action-object-to-express-cleanup-if-no-suitable-resource-handle-is-available


          新書介紹

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


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

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




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

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

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



          瀏覽 27
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  美女福利视频网 | 豆花视频网站国产 | 超碰人妻中文字幕 | 久久久久久电影成人电影 | 91北条麻妃二区 |