vault backup: 2022-06-09 14:40:26
Affected files: .obsidian/workspace 02. PARA/03. Resources(資源)/C++17/rvalue.md 02. PARA/03. Resources(資源)/C++17/智慧指標.md
This commit is contained in:
8
.obsidian/workspace
vendored
8
.obsidian/workspace
vendored
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -60,6 +60,15 @@ std::unique_ptr<int> ptr2 = ptr1; // Error
|
||||
在Visual Studio 2017上,錯誤訊息是這樣:`error C2280: 'std::unique_ptr<int,std::default_delete<int>>::unique_ptr(const std::unique_ptr<int,std::default_delete<int>> &)': 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<int>(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):
|
||||
|
||||
Reference in New Issue
Block a user