C++ 借來的資源,如何還的瀟灑?

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"正文"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所謂的資源就是,一旦用了它,將來必須還給系統。如果不是這樣,糟糕的事情就會發生。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C++ 程序內常見的資源:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"動態分配內存"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文件描述符"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"互斥鎖"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"圖形頁面中的字型和筆刷"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據庫連接"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網絡 sockets"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"無論哪一種資源,重要的是,當你不再使用它時,必須將它還給系統,有借有還是個好習慣。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"細節 01 : 以對象管理資源"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"把資源放在析構函數,交給析構函數釋放資源"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設某個 class 含有個工廠函數,該函數獲取了對象的指針:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"A* createA(); // 返回指針,指向的是動態分配對象。\n // 調用者有責任刪除它。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如上述註釋所言,createA 的調用端使用了函數返回的對象後,有責任刪除它。現在考慮有個"},{"type":"codeinline","content":[{"type":"text","text":"f"}]},{"type":"text","text":"函數履行了這個責任:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"void f()\n{\n A *pa = createA(); // 調用工廠函數\n ... // 其他代碼\n delete pa; // 釋放資源\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這看起來穩妥,但存在若干情況"},{"type":"codeinline","content":[{"type":"text","text":"f"}]},{"type":"text","text":"函數可能無法執行到"},{"type":"codeinline","content":[{"type":"text","text":"delete pa"}]},{"type":"text","text":"語句,也就會造成"},{"type":"text","marks":[{"type":"strong"}],"text":"資源泄漏"},{"type":"text","text":",例如如下情況:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"或許因爲「...」區域內的一個過早的 return 語句;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"或許因爲「...」區域內的一個循環語句過早的continue 或 goto 語句退出;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"或許因爲「...」區域內的語句拋出異常,無法執行到 delete。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當然可以通過謹慎地編寫程序可以防止這一類錯誤,但你必須想想,代碼可能會在時間漸漸過去後被修改,如果是一個新手沒有注意這一類情況,那必然又會再次有內存泄漏的可能性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲確保 A 返回的資源都是被回收,我們需要將資源放進對象內,當對象離開作用域時,該對象的析構函數會自動釋放資源。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"「智能指針」是個好幫手,交給它去管理指針對象。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"對於是由動態分配(new)於堆內存的對象,指針對象離開了作用域並不會自動調用析構函數(需手動delete)"},{"type":"text","text":",爲了讓指針對象能像普通對象一樣,離開作用域自動調用析構函數回收資源,我們需要藉助「智能指針」的特性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"常用的「智能指針」有如下三個:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"std::auto_ptr( C++ 98 提供、C++ 11 建議摒棄不用 )"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"std::unique_ptr( C++ 11 提供 )"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"std::shared_ptr( C++ 11 提供 )"}]}]}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"std::auto_ptr"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面示範如何使用 std::auto_ptr 以避免 "},{"type":"codeinline","content":[{"type":"text","text":"f"}]},{"type":"text","text":" 函數潛在的資源泄漏可能性:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"void f()\n{\n std::auto_ptr pa (createA()); // 調用工廠函數\n ... // 一如既往的使用pa\n} // 離開作用域後,經由 auto_ptr 的析構函數自動刪除pa;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個簡單的例子示範「以對象管理資源」的兩個關鍵想法:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"獲得資源後立刻放進管理對象內"},{"type":"text","text":"。以上代碼中 createA 返回的資源被當做其管理者 auto_ptr 的初值,也就立刻被放進了管理對象中。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"管理對象運用析構函數確保資源釋放"},{"type":"text","text":"。不論控制流如何離開區塊,一旦對象被銷燬(例如當對象離開作用域)其析構函數自然會被自動調用,於是資源被釋放。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲什麼在 C++11 建議棄用 auto"},{"type":"text","marks":[{"type":"italic"}],"text":"ptr 嗎?當然是 auto"},{"type":"text","text":"ptr 存在缺陷,所以後續不被建議使用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"auto_ptr 有一個不尋常的特質:若通過「複製構造函數或賦值操作符函數」 "},{"type":"text","marks":[{"type":"strong"}],"text":"copy"},{"type":"text","text":" 它們,它們會變成 "},{"type":"text","marks":[{"type":"strong"}],"text":"null"},{"type":"text","text":" ,而複製所得的指針將獲取資源的唯一擁有權!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"見如下例子說明:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"std::auto_ptr pa1(createA()); // pa1 指向 createA 返回物\n\nstd::auto_ptr pa2(pa1); // 現在 pa2 指向對象,pa1將被設置爲 null\n\npa1 = pa2; // 現在 pa1 指向對象,pa2 將被設置爲 null"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這一詭異的複製行爲,如果再次使用指向爲 null 的指針,那必然會導致程序"},{"type":"text","marks":[{"type":"strong"}],"text":"奔潰"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"意味着 auto_ptr 並非管理動態分配資源的神兵利器。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"std::unique_ptr"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"unique_ptr 也採用所有權模型,但是在使用時,是直接"},{"type":"text","marks":[{"type":"strong"}],"text":"禁止"},{"type":"text","text":"通過複製構造函數或賦值操作符函數 copy 指針對象,如下例子在編譯時,會出錯:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"std::unique_ptr pa1(createA()); // pa1 指向 createA 返回物\n\nstd::unique_ptr pa2(pa1); // 編譯出錯!\n\npa1 = pa2; // 編譯出錯!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"std::shared_ptr"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"shared_ptr 在使用複製構造函數或賦值操作符函數後,"},{"type":"text","marks":[{"type":"strong"}],"text":"引用計會數累加並且兩個指針對象指向的都是同一個塊內存"},{"type":"text","text":",這就與 unique"},{"type":"text","marks":[{"type":"italic"}],"text":"ptr、auto"},{"type":"text","text":"ptr 不同之處。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"void f()\n{\n std::shared_ptr pa1(createA()); // pa1 指向 createA 返回物\n \n std::shared_ptr pa2(pa1); // 引用計數+1,pa2和pa1指向同一個內存\n \n pa1 = pa2; // 引用計數+1,pa2和pa1指向同一個內存\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當一個對象離開作用域,shared_ptr 會把引用計數值 -1 ,"},{"type":"text","marks":[{"type":"strong"}],"text":"直到引用計數值爲 0 時,纔會進行刪除對象。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由於 shared_ptr 釋放空間時會事先要判斷"},{"type":"text","marks":[{"type":"strong"}],"text":"引用計數值的大小"},{"type":"text","text":",因此不會出現多次刪除一個對象的錯誤。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"小結 - 請記住"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲防止資源泄漏,請使用 RAII(Resource Acquisition Is Initaliaztion - 資源取得時機便是初始化時機) 對象,它們在構造函數中獲取資源,並在析構函數中是釋放資源"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"兩個建議使用的 RAII classes 分別是 std::unique"},{"type":"text","marks":[{"type":"italic"}],"text":"ptr 和 std::shared"},{"type":"text","text":"ptr。前者不允許 copy 動作,後者允許 copy 動作。但是不建議用 std::auto"},{"type":"text","marks":[{"type":"italic"}],"text":"ptr,若選 auto"},{"type":"text","text":"ptr,複製動作會使它(被複制物)指向 null 。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"細節 02:在資源管理類中小心 copying 行爲"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設,我們使用 C 語音的 API 函數處理類型爲 Mutex 的互斥對象,共有 lock 和 unlock 兩函數可用:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"void locak(Mutex *pm); // 鎖定 pm 所指的互斥器\nvoid unlock(Mutex* pm); // 將互斥器解除鎖定"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲確保絕不會忘記一個被鎖住的 Mutex 解鎖,我們可能會希望創立一個 class 來管理鎖資源。這樣的 class 要遵守 RAII 守則,也就是「資源在構造期間獲得,在析構釋放期間釋放」:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"class Lock\n{\npublic:\n explicit Lock(Mutex *pm) // 構造函數\n : pMutex(pm)\n {\n lock(pMutex);\n }\n \n ~Lock() // 析構函數\n {\n unlock(pMutex);\n }\nprivate:\n Mutex* pMutex;\n};"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這樣定義的 Lock,用法符合 RAII 方式:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"Mutex m; //定義你需要的互斥鎖\n... \n{ // 建立一個局部區塊作用域\n Lock m1(&m); // 鎖定互斥器\n ...\n} // 在離開區塊作用域,自動解除互斥器鎖定"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這很好,但如果 Lock 對象被複制,會發生什麼事情?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"Lock m1(&m); // 鎖定m\nLock m2(&m1); // 將 m1 複製到 m2身上,這會發生什麼?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這是我們需要思考和麪對的:「當一個 RAII 對象被複制,會發生什麼事情?」大多數時候你會選擇以下兩種可能:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"禁止複製"},{"type":"text","text":"。如果 RAII 不允許被複制,那我們需要將 class 的複製構造函數和賦值操作符函數聲明在 private。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"使用引用計數法"},{"type":"text","text":"。有時候我們希望保有資源,直到它直的最後一個對象被消耗。這種情況下複製 RAII 對象時,應該將資源的「被引用數」遞增。std::shared_ptr 便是如此。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果前述的 Lock 打算使用"},{"type":"text","marks":[{"type":"strong"}],"text":"使用引用計數法"},{"type":"text","text":",它可以使用 std::shared"},{"type":"text","marks":[{"type":"italic"}],"text":"ptr 來管理 pMutex 指針,然後很不幸 std::shared"},{"type":"text","text":"ptr 的默認行爲是「當引用次數爲 0 時刪除其所指物」那不是我們想要的行爲,"},{"type":"text","marks":[{"type":"strong"}],"text":"因爲要對 Mutex 釋放動作是解鎖而非刪除。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"幸運的是 std::shared_ptr 允許指定"},{"type":"text","marks":[{"type":"strong"}],"text":"自定義的刪除方式"},{"type":"text","text":",那是一個函數或函數對象。如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"class Lock\n{\npublic:\n explicit Lock(Mutex *pm) \n : pMutex(pm, unlock) // 以某個 Mutex 初始化 shared_ptr,\n // 並以 unlock 函數爲刪除器。\n {\n lock(pMutex.get()); // get 獲取指針地址\n }\n \nprivate:\n std::shared_ptr pMutex; // 使用 shared_ptr\n};"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"請注意,本例的 Lock class 不再聲明析構函數。因爲編譯器會自動創立默認的析構函數,來自動調用其 non-static 成員變量(本例爲 pMutex )的析構函數。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而 pMutex 的析構函數會"},{"type":"text","marks":[{"type":"strong"}],"text":"在互斥器的引用次數爲 0 時,自動調用 std::shared_ptr 的刪除器(本例爲 unlock )"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"小結 - 請記住"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"複製 RAII 對象必須一併複製它的所管理的資源(深拷貝),所以資源的 copying 行爲決定 RAII 對象的 copying 行爲。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"普通而常見的 RAII class copying 行爲是:禁止 copying、施行引用計數法。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"細節 03 :在資源類中提供對原始資源的訪問"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"智能指針「顯式」轉換,也就是通過 get 成員函數的方式轉換爲原始指針對象。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面提到的「智能指針」分別是:std::auto"},{"type":"text","marks":[{"type":"italic"}],"text":"ptr、std::unique"},{"type":"text","text":"ptr、std::shared_ptr。它們都有訪問原始資源的辦法,都提供了一個 get 成員函數,用來執行"},{"type":"text","marks":[{"type":"strong"}],"text":"顯式轉換"},{"type":"text","text":",也就是它會"},{"type":"text","marks":[{"type":"strong"}],"text":"返回智能指針內部的原始指針(的復件)"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"舉個例子,使用智能指針如 std::shared_ptr 保存 createA() 返回的指針對象 :"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"std::shared_ptr pA(createA());"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設你希望以某個函數處理 A 對象,像這樣:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"int getInfo(const A* pA);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你想這麼調用它:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"std::shared_ptr pA(createA());\ngetInfo(pA); // 錯誤!!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"會編譯錯誤,因爲 getInfo 需要的是 "},{"type":"codeinline","content":[{"type":"text","text":"A"}]},{"type":"text","text":" 指針對象,而不是類型爲 "},{"type":"codeinline","content":[{"type":"text","text":"std::shared_ptr"}]},{"type":"text","text":" 的對象。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這時候就需要用 std::shared_ptr 智能指針提供的 "},{"type":"codeinline","content":[{"type":"text","text":"get"}]},{"type":"text","text":" 成員函數訪問原始的資源:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"std::shared_ptr pA(createA());\ngetInfo(pA.get()); // 很好,將 pA 內的原始指針傳遞給 getInfo"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"智能指針「隱式」轉換的方式,是通過指針取值操作符。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"智能指針都重載了指針取值操作符(operator->和operator*),它們允許"},{"type":"text","marks":[{"type":"strong"}],"text":"隱式轉換"},{"type":"text","text":"至底部原始指針:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"class A\n{\npublic:\n bool isExist() const;\n ...\n};\n\nA* createA(); // 工廠函數,創建指針對象\n\nstd::shared_ptr pA(createA()); // 令 shared_ptr 管理對象資源\n\nbool exist = pA->isExist(); // 經由 operator-> 訪問資源\nbool exist2 = (*pA).isExist(); // 經由 operator* 訪問資源"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"多數設計良好的 classes 一樣,它隱藏了程序員不需要看到的部分,但是有程序員需要的所有東西。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以對於自身設計 RAII classes 我們也要提供一個「取得其所管理的資源」的辦法。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"小結 - 請記住"}]}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"APIs 往往要求訪問原始資源,所以每一個 RAII class 應該提供一個「取得其所管理的資源」的辦法。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對原始資源的訪問可能經由顯式轉換或隱式轉換。一般而言顯式轉換比較安全,但隱式轉換比較方便。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"細節 04:成對使用 new 和 delete "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以下動作有什麼錯?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"std::string* strArray = new std::string[100];\n...\ndelete strArray;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每件事情看起來都井然有序。使用了 new,也搭配了對應的 delete。但還是有某樣東西完全錯誤。strArray 所含的 100 個 string 對象中的 99 個不太可能被適當刪除,因爲它們的析構函數很可能沒有被調用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當使用 new ,有兩件事發生:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"內存被分配出來(通過名爲 operator new 的函數)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"針對此內存會有"},{"type":"text","marks":[{"type":"strong"}],"text":"一個或多個"},{"type":"text","text":"構造函數被調用"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當使用 delete,也會有兩件事情:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"針對此內存會有"},{"type":"text","marks":[{"type":"strong"}],"text":"一個或多個"},{"type":"text","text":"析構函數被調用"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"然後內存才被釋放(通過名爲 operator delete 的函數)"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"delete 的最大問題在於:"},{"type":"text","marks":[{"type":"strong"}],"text":"即將被刪除的內存之內究竟有多少對象?這個答案決定了需要執行多少個析構函數。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對象數組所用的內存通常還包括「數組大小」的記錄,以便 delete 知道需要調用多少次析構函數。單一對象的內存則沒有這筆記錄。你可以把兩者不同的內存佈局想象如下,其中 n 是數組大小:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當你對着一個指針使用 delete,唯一能夠讓 delete 知道內存中是否存在一個「數組大小記錄」的辦法就是:由你告訴它。如果你使用 delete 時加上中括號"},{"type":"text","marks":[{"type":"strong"}],"text":"[]"},{"type":"text","text":",delete 便認定指針指向一個數組,否則它便認定指針指向一個單一對象:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"std::string* strArray = new std::string[100];\nstd::string* strPtr = new std::strin;\n... \ndelete [] strArray; // 刪除一個對象\ndelete strPtr; // 刪除一個由對象組成的數組"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"遊戲規則很簡單:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你在 new 表達式中"},{"type":"text","marks":[{"type":"strong"}],"text":"使用[]"},{"type":"text","text":",必須在相應的 delete 表達式也"},{"type":"text","marks":[{"type":"strong"}],"text":"使用[]"},{"type":"text","text":"。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你在 new 表達式中"},{"type":"text","marks":[{"type":"strong"}],"text":"不使用[]"},{"type":"text","text":",一定"},{"type":"text","marks":[{"type":"strong"}],"text":"不要"},{"type":"text","text":"在相應的 delete 表達式"},{"type":"text","marks":[{"type":"strong"}],"text":"使用[]"},{"type":"text","text":"。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"小結 - 請記住"}]}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你在 new 表達式中使用[],必須在相應的 delete 表達式也使用[]。如果你在 new 表達式中不使用[],一定不要在相應的 delete 表達式使用[]。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"細節 05:以獨立語句將 newed (已被 new 的)對象置入智能指針"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設我們有個以下示範的函數:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"int getNum();\nvoid fun(std::shared_ptr pA, int num);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在考慮調用 fun:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"fun(new A(), getNum());"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"它不能通過編譯,因爲 "},{"type":"codeinline","content":[{"type":"text","text":"std::shared_ptr"}]},{"type":"text","text":" 構造函數需要一個原始指針,而且該構造函數是個 "},{"type":"codeinline","content":[{"type":"text","text":"explicit"}]},{"type":"text","text":" 構造函數,無法進行隱式轉換。如果寫成這樣就可以編譯通過:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"fun(std::shared_ptr(new A), getNum());"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"令人想不到吧,上述調用卻可能"},{"type":"text","marks":[{"type":"strong"}],"text":"泄露資源"},{"type":"text","text":"。接下來我們來一步一步的分析爲什麼存在內存泄漏的可能性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在進入 "},{"type":"codeinline","content":[{"type":"text","text":"fun"}]},{"type":"text","text":" 函數之前,肯定會先執行各個實參。上述第二個實參只是單純的對 "},{"type":"codeinline","content":[{"type":"text","text":"getNum"}]},{"type":"text","text":" 函數的調用,但第一個實參 "},{"type":"codeinline","content":[{"type":"text","text":"std::shared_ptr(new A)"}]},{"type":"text","text":" 由兩部分組成:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"執行 "},{"type":"codeinline","content":[{"type":"text","text":"new A"}]},{"type":"text","text":" 表達式"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"調用 "},{"type":"codeinline","content":[{"type":"text","text":"std::shared_ptr"}]},{"type":"text","text":" 構造函數"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"於是在調用 "},{"type":"codeinline","content":[{"type":"text","text":"fun"}]},{"type":"text","text":" 函數之前,先必須做以下三件事:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"調用 "},{"type":"codeinline","content":[{"type":"text","text":"getNum"}]},{"type":"text","text":" 函數"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"執行 "},{"type":"codeinline","content":[{"type":"text","text":"new A"}]},{"type":"text","text":" 表達式"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"調用 "},{"type":"codeinline","content":[{"type":"text","text":"std::shared_ptr"}]},{"type":"text","text":" 構造函數"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼他們的執行次序是一定如上述那樣的嗎?可以確定的是 "},{"type":"codeinline","content":[{"type":"text","text":"new A"}]},{"type":"text","text":" 一定比 "},{"type":"codeinline","content":[{"type":"text","text":"std::shared_ptr"}]},{"type":"text","text":" 構造函數先被執行。但對 getNum 調用可以排在第一或第二或第三執行。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果編譯器選擇以第二順位執行它:"}]},{"type":"numberedlist","attrs":{"start":"1","normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"執行 "},{"type":"codeinline","content":[{"type":"text","text":"new A"}]},{"type":"text","text":" 表達式"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"調用 "},{"type":"codeinline","content":[{"type":"text","text":"getNum"}]},{"type":"text","text":" 函數"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"調用 "},{"type":"codeinline","content":[{"type":"text","text":"std::shared_ptr"}]},{"type":"text","text":" 構造函數"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"萬一在調用 "},{"type":"codeinline","content":[{"type":"text","text":"getNum"}]},{"type":"text","text":" 函數發生了異常,會發生什麼事情?在此情況下 "},{"type":"codeinline","content":[{"type":"text","text":"new A"}]},{"type":"text","text":" 返回的指針將不會置入 "},{"type":"codeinline","content":[{"type":"text","text":"std::shared_ptr"}]},{"type":"text","text":" 智能指針裏,就"},{"type":"text","marks":[{"type":"strong"}],"text":"存在內存泄漏的現象"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"避免這類問題的辦法很簡單:使用"},{"type":"text","marks":[{"type":"strong"}],"text":"分離語句"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"分別寫出:"}]},{"type":"numberedlist","attrs":{"start":"1","normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"創建 A"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"將它置入一個智能指針內"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"然後再把智能指針傳遞給 "},{"type":"codeinline","content":[{"type":"text","text":"fun"}]},{"type":"text","text":" 函數。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"std::shared_ptr pA(new A); // 先構造智能指針對象\nfun(pA, getNum()); // 這個調用動作絕不至於造成泄漏。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上的方式,就能避免原本由於次序導致內存泄漏發生。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"小結 - 請記住"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以獨立語句將 newed (已 new 過) 對象存儲於智能指針內。如果不這樣做,一旦異常被拋出,有可能導致難以察覺的資源泄漏。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"最後"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文部分內容參考了《Effective C++ (第3版本)》第三章節內容,前兩章節的內容可看舊文"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"《"},{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s/3dUQpmKsA-6LHopYY9u4HQ","title":""},"content":[{"type":"text","text":"學過 C++ 的你,不得不知的這 10 條細節!"}]},{"type":"text","text":"》"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c5/c5a651ce460ea82cc1e4a612c6a374ab.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章