From 5f5e32ea884d52141205bcdbca985f2e795c3846 Mon Sep 17 00:00:00 2001 From: Awin Huang Date: Thu, 9 Jun 2022 13:50:26 +0800 Subject: [PATCH] vault backup: 2022-06-09 13:50:26 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Affected files: 02. PARA/03. Resources(資源)/C++17/rvalue.md --- .../03. Resources(資源)/C++17/rvalue.md | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/02. PARA/03. Resources(資源)/C++17/rvalue.md b/02. PARA/03. Resources(資源)/C++17/rvalue.md index c7603f3..0ffb5d0 100644 --- a/02. PARA/03. Resources(資源)/C++17/rvalue.md +++ b/02. PARA/03. Resources(資源)/C++17/rvalue.md @@ -3,12 +3,41 @@ rvalue 是指: - 臨時的值,例如運算的結果 - 無法被取址(address-of)的物件 -可以用`&&`來參考rvalue。例如: +## rvalue reference +一般的參考只能參考[[lvalue]],如下的程式是ok的: ```cpp -int c{5}; -int&& rtepm {c + 3}; // rtepm等於c + 3的結果 +int a = 10; +int& b = a; ``` +但是像這樣就不行了: +```cpp +int a = 10; +int b = 5; +int& c = a + b; +``` + +因為`a+b`是一個rvalue(臨時的值,沒辦法取址),所以無法參考。 +但是可以用`&&`來參考rvalue。例如: +```cpp +int a = 10; +int b = 5; +int&& c = a + b; // c = 15 +``` + +而不用這樣: +```cpp +int a = 10; +int b = 5; +int r = a + b; +int& c = r; +``` + +了解rvalue reference之後,就可以實作類別的 move constructor 跟move assignment operator。 + +## Move constructor +Mov + ## 參考