vault backup: 2022-06-09 14:50:27
Affected files: .obsidian/workspace 02. PARA/03. Resources(資源)/C++17/rvalue.md 02. PARA/03. Resources(資源)/C++17/智慧指標.md
This commit is contained in:
12
.obsidian/workspace
vendored
12
.obsidian/workspace
vendored
@@ -10,7 +10,7 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "markdown",
|
"type": "markdown",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "02. PARA/03. Resources(資源)/C++17/智慧指標.md",
|
"file": "02. PARA/03. Resources(資源)/C++17/rvalue.md",
|
||||||
"mode": "source",
|
"mode": "source",
|
||||||
"source": true
|
"source": true
|
||||||
}
|
}
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "outline",
|
"type": "outline",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "02. PARA/03. Resources(資源)/C++17/智慧指標.md"
|
"file": "02. PARA/03. Resources(資源)/C++17/rvalue.md"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,7 +81,7 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "backlink",
|
"type": "backlink",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "02. PARA/03. Resources(資源)/C++17/智慧指標.md",
|
"file": "02. PARA/03. Resources(資源)/C++17/rvalue.md",
|
||||||
"collapseAll": true,
|
"collapseAll": true,
|
||||||
"extraContext": true,
|
"extraContext": true,
|
||||||
"sortOrder": "alphabetical",
|
"sortOrder": "alphabetical",
|
||||||
@@ -125,15 +125,15 @@
|
|||||||
},
|
},
|
||||||
"active": "94f0e8a81af6c9e2",
|
"active": "94f0e8a81af6c9e2",
|
||||||
"lastOpenFiles": [
|
"lastOpenFiles": [
|
||||||
"02. PARA/03. Resources(資源)/C++17/智慧指標.md",
|
|
||||||
"02. PARA/03. Resources(資源)/C++17/rvalue.md",
|
"02. PARA/03. Resources(資源)/C++17/rvalue.md",
|
||||||
|
"02. PARA/03. Resources(資源)/C++17/move operator.md",
|
||||||
|
"02. PARA/03. Resources(資源)/C++17/智慧指標.md",
|
||||||
"00. TOP/01. TODO.md",
|
"00. TOP/01. TODO.md",
|
||||||
"02. PARA/03. Resources(資源)/git/submodule.md",
|
"02. PARA/03. Resources(資源)/git/submodule.md",
|
||||||
"02. PARA/03. Resources(資源)/git.md",
|
"02. PARA/03. Resources(資源)/git.md",
|
||||||
"02. PARA/01. Project(專案)/008. Sentinel.md",
|
"02. PARA/01. Project(專案)/008. Sentinel.md",
|
||||||
"02. PARA/03. Resources(資源)/C++17/lvalue.md",
|
"02. PARA/03. Resources(資源)/C++17/lvalue.md",
|
||||||
"02. PARA/03. Resources(資源)/FFMPEG/01. Setup.md",
|
"02. PARA/03. Resources(資源)/FFMPEG/01. Setup.md",
|
||||||
"02. PARA/03. Resources(資源)/FFMPEG/00. Introduction.md",
|
"02. PARA/03. Resources(資源)/FFMPEG/00. Introduction.md"
|
||||||
"02. PARA/03. Resources(資源)/FFMpeg.md"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -123,7 +123,7 @@ BigBuffer move constructor // move buf1 to buf2, buf1 has nullptr now
|
|||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
另外一個情形也可以受益於此,假如我們有個function會產生 BigBuffer,如下:
|
另外一個情形也可以受益於此,假如我們有個 function 會產生 `BigBuffer`,如下:
|
||||||
```cpp
|
```cpp
|
||||||
BigBuffer BigBufferCreator() {
|
BigBuffer BigBufferCreator() {
|
||||||
std::cout << "BigBufferCreator: Create a BigBuffer!\n";
|
std::cout << "BigBufferCreator: Create a BigBuffer!\n";
|
||||||
@@ -136,7 +136,7 @@ BigBuffer BigBufferCreator() {
|
|||||||
BigBuffer b = BigBufferCreator(); // copy tempb to b
|
BigBuffer b = BigBufferCreator(); // copy tempb to b
|
||||||
```
|
```
|
||||||
|
|
||||||
在沒有Move constructor的情況下,上面的程式會先產生一個 tempb,然後複製給 b,訊息:
|
在沒有 Move constructor 的情況下,上面的程式會先產生一個 `tempb`,然後複製給 `b`,訊息:
|
||||||
```
|
```
|
||||||
BigBufferCreator: Create a BigBuffer!
|
BigBufferCreator: Create a BigBuffer!
|
||||||
BigBuffer constructor
|
BigBuffer constructor
|
||||||
@@ -155,10 +155,10 @@ BigBuffer destructor
|
|||||||
BigBuffer destructor
|
BigBuffer destructor
|
||||||
```
|
```
|
||||||
|
|
||||||
因為 `BigBufferCreator()` 產生的就是一個 BigBuffer rvalue,所以 compiler會使用 move constructor(`BigBuffer(BigBuffer&& src)`) 而不是 copy constructor。
|
因為 `BigBufferCreator()` 產生的就是一個 `BigBuffer` rvalue,所以 compiler 會使用 move constructor(`BigBuffer(BigBuffer&& src)`) 而不是 copy constructor。
|
||||||
|
|
||||||
## Move assignment operator(`=`)
|
## Move assignment operator(`=`)
|
||||||
Move assignment operator 的行為跟 move constructor 是一樣的,幫 BigBuffer 加入 move assignment operator:
|
Move assignment operator 的行為跟 move constructor 是一樣的,幫 `BigBuffer` 加入 move assignment operator:
|
||||||
```cpp
|
```cpp
|
||||||
class BigBuffer {
|
class BigBuffer {
|
||||||
public:
|
public:
|
||||||
@@ -196,8 +196,15 @@ BigBuffer b1, b2;
|
|||||||
b2 = std::move(b1);
|
b2 = std::move(b1);
|
||||||
```
|
```
|
||||||
|
|
||||||
這樣就可以了。
|
這樣就可以了。訊息:
|
||||||
|
```
|
||||||
|
BigBuffer constructor
|
||||||
|
BigBuffer constructor
|
||||||
|
BigBuffer move operator // Use MOVE!
|
||||||
|
```
|
||||||
|
|
||||||
## 參考
|
## 參考
|
||||||
- [Value categories - cppreference.com](https://en.cppreference.com/w/cpp/language/value_category)
|
- [Value categories - cppreference.com](https://en.cppreference.com/w/cpp/language/value_category)
|
||||||
- [rvalue 參考](https://openhome.cc/Gossip/CppGossip/RvalueReference.html)
|
- [rvalue 參考](https://openhome.cc/Gossip/CppGossip/RvalueReference.html)
|
||||||
|
- [Move constructors - cppreference.com](https://en.cppreference.com/w/cpp/language/move_constructor)
|
||||||
|
- [Move assignment operator - cppreference.com](https://en.cppreference.com/w/cpp/language/move_assignment)
|
||||||
@@ -68,7 +68,7 @@ auto ptr1 = std::make_unique<int>(5);
|
|||||||
auto anotherPtr = std::move(ptr1);
|
auto anotherPtr = std::move(ptr1);
|
||||||
```
|
```
|
||||||
|
|
||||||
ptr1原本所管理的指標會轉移給 anotherPtr,
|
ptr1原本所管理的指標會轉移給 anotherPtr,ptr1 會變成 nullptr。
|
||||||
|
|
||||||
## shared_ptr
|
## shared_ptr
|
||||||
建立一個 `shared_ptr` 是使用[`std::make_shared()`](https://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared):
|
建立一個 `shared_ptr` 是使用[`std::make_shared()`](https://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared):
|
||||||
|
|||||||
Reference in New Issue
Block a user