From a7a1b5b6506613e97402ffdf2e6b6dba3571ebb8 Mon Sep 17 00:00:00 2001 From: Awin Huang Date: Thu, 9 Jun 2022 14:40:26 +0800 Subject: [PATCH] vault backup: 2022-06-09 14:40:26 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Affected files: .obsidian/workspace 02. PARA/03. Resources(資源)/C++17/rvalue.md 02. PARA/03. Resources(資源)/C++17/智慧指標.md --- .obsidian/workspace | 8 ++++---- 02. PARA/03. Resources(資源)/C++17/rvalue.md | 8 +++++++- 02. PARA/03. Resources(資源)/C++17/智慧指標.md | 9 +++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.obsidian/workspace b/.obsidian/workspace index 7e4c6a6..fb2e6ea 100644 --- a/.obsidian/workspace +++ b/.obsidian/workspace @@ -10,7 +10,7 @@ "state": { "type": "markdown", "state": { - "file": "02. PARA/03. Resources(資源)/C++17/rvalue.md", + "file": "02. PARA/03. Resources(資源)/C++17/智慧指標.md", "mode": "source", "source": true } @@ -23,7 +23,7 @@ "state": { "type": "outline", "state": { - "file": "02. PARA/03. Resources(資源)/C++17/rvalue.md" + "file": "02. PARA/03. Resources(資源)/C++17/智慧指標.md" } } } @@ -81,7 +81,7 @@ "state": { "type": "backlink", "state": { - "file": "02. PARA/03. Resources(資源)/C++17/rvalue.md", + "file": "02. PARA/03. Resources(資源)/C++17/智慧指標.md", "collapseAll": true, "extraContext": true, "sortOrder": "alphabetical", @@ -125,12 +125,12 @@ }, "active": "94f0e8a81af6c9e2", "lastOpenFiles": [ + "02. PARA/03. Resources(資源)/C++17/智慧指標.md", "02. PARA/03. Resources(資源)/C++17/rvalue.md", "00. TOP/01. TODO.md", "02. PARA/03. Resources(資源)/git/submodule.md", "02. PARA/03. Resources(資源)/git.md", "02. PARA/01. Project(專案)/008. Sentinel.md", - "02. PARA/03. Resources(資源)/C++17/智慧指標.md", "02. PARA/03. Resources(資源)/C++17/lvalue.md", "02. PARA/03. Resources(資源)/FFMPEG/01. Setup.md", "02. PARA/03. Resources(資源)/FFMPEG/00. Introduction.md", diff --git a/02. PARA/03. Resources(資源)/C++17/rvalue.md b/02. PARA/03. Resources(資源)/C++17/rvalue.md index a8c2cd9..0143ee7 100644 --- a/02. PARA/03. Resources(資源)/C++17/rvalue.md +++ b/02. PARA/03. Resources(資源)/C++17/rvalue.md @@ -190,7 +190,13 @@ BigBuffer constructor BigBuffer copy operator, copy 104857600Bytes ``` -還是使用 copy operator +還是使用 copy assignment operator 來複製,理由是一樣的,需要一個明確的 `std::move()` 來表示「轉移」的行動,把程式改成: +```cpp +BigBuffer b1, b2; +b2 = std::move(b1); +``` + +這樣就可以了。 ## 參考 - [Value categories - cppreference.com](https://en.cppreference.com/w/cpp/language/value_category) diff --git a/02. PARA/03. Resources(資源)/C++17/智慧指標.md b/02. PARA/03. Resources(資源)/C++17/智慧指標.md index 2e2b28b..b1b59b6 100644 --- a/02. PARA/03. Resources(資源)/C++17/智慧指標.md +++ b/02. PARA/03. Resources(資源)/C++17/智慧指標.md @@ -60,6 +60,15 @@ std::unique_ptr ptr2 = ptr1; // Error 在Visual Studio 2017上,錯誤訊息是這樣:`error C2280: 'std::unique_ptr>::unique_ptr(const std::unique_ptr> &)': attempting to reference a deleted function`。 其實就是`unique_ptr`的copy constructor跟assignment operator都被標記為delete了。 +### Move a unique_ptr +如果一定要把 unique_ptr 指定給別人可以嗎?可以的,用 `std::move()` 來轉移: +```cpp +auto ptr1 = std::make_unique(5); +// do something +auto anotherPtr = std::move(ptr1); +``` + +ptr1原本所管理的指標會轉移給 anotherPtr, ## shared_ptr 建立一個 `shared_ptr` 是使用[`std::make_shared()`](https://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared):