File tree 4 files changed +18
-4
lines changed
4 files changed +18
-4
lines changed Original file line number Diff line number Diff line change 1
1
#include " ../exercise.h"
2
2
3
3
// READ: 析构函数 <https://zh.cppreference.com/w/cpp/language/destructor>
4
+ // READ: RAII <https://learn.microsoft.com/zh-cn/cpp/cpp/object-lifetime-and-resource-management-modern-cpp?view=msvc-170>
4
5
5
6
// / @brief 任意缓存容量的斐波那契类型。
6
7
// / @details 可以在构造时传入缓存容量,因此需要动态分配缓存空间。
Original file line number Diff line number Diff line change 1
1
#include " ../exercise.h"
2
2
3
3
// READ: 复制构造函数 <https://zh.cppreference.com/w/cpp/language/copy_constructor>
4
+ // READ: 函数定义(显式弃置)<https://zh.cppreference.com/w/cpp/language/function>
5
+
4
6
5
7
class DynFibonacci {
6
8
size_t *cache;
@@ -11,7 +13,7 @@ class DynFibonacci {
11
13
DynFibonacci (int capacity): cache(new ?), cached(?) {}
12
14
13
15
// TODO: 实现复制构造器
14
- DynFibonacci (DynFibonacci const &other ) = delete ;
16
+ DynFibonacci (DynFibonacci const &) = delete ;
15
17
16
18
// TODO: 实现析构器,释放缓存空间
17
19
~DynFibonacci ();
Original file line number Diff line number Diff line change 1
1
#include " ../exercise.h"
2
2
3
+ // READ: 左值右值(概念)<https://learn.microsoft.com/zh-cn/cpp/c-language/l-value-and-r-value-expressions?view=msvc-170>
4
+ // READ: 左值右值(细节)<https://zh.cppreference.com/w/cpp/language/value_category>
5
+ // READ: 关于移动语义 <https://learn.microsoft.com/zh-cn/cpp/cpp/rvalue-reference-declarator-amp-amp?view=msvc-170#move-semantics>
6
+ // READ: 如果实现移动构造 <https://learn.microsoft.com/zh-cn/cpp/cpp/move-constructors-and-move-assignment-operators-cpp?view=msvc-170>
7
+
3
8
// READ: 移动构造函数 <https://zh.cppreference.com/w/cpp/language/move_constructor>
4
9
// READ: 移动赋值 <https://zh.cppreference.com/w/cpp/language/move_assignment>
5
10
// READ: 运算符重载 <https://zh.cppreference.com/w/cpp/language/operators>
@@ -13,11 +18,11 @@ class DynFibonacci {
13
18
DynFibonacci (int capacity): cache(new ?), cached(?) {}
14
19
15
20
// TODO: 实现移动构造器
16
- DynFibonacci (DynFibonacci &&other ) noexcept = delete ;
21
+ DynFibonacci (DynFibonacci &&) noexcept = delete ;
17
22
18
23
// TODO: 实现移动赋值
19
24
// NOTICE: ⚠ 注意移动到自身问题 ⚠
20
- DynFibonacci &operator =(DynFibonacci &&other ) noexcept = delete ;
25
+ DynFibonacci &operator =(DynFibonacci &&) noexcept = delete ;
21
26
22
27
// TODO: 实现析构器,释放缓存空间
23
28
~DynFibonacci ();
@@ -30,6 +35,12 @@ class DynFibonacci {
30
35
return cache[i];
31
36
}
32
37
38
+ // NOTICE: 不要修改这个方法
39
+ size_t operator [](int i) const {
40
+ ASSERT (i <= cached, " i out of range" );
41
+ return cache[i];
42
+ }
43
+
33
44
// NOTICE: 不要修改这个方法
34
45
bool is_alive () const {
35
46
return cache;
Original file line number Diff line number Diff line change @@ -59,7 +59,7 @@ int main(int argc, char **argv) {
59
59
// 这是不可能的,A 无法提供 B 增加的成员变量的值
60
60
// B ba = A(4);
61
61
62
- // 这也是不可能的,因为 A 是 B 的一部分,就好像不可以把套娃🪆的外层放进内层里 。
62
+ // 这也是不可能的,因为 A 是 B 的一部分,就好像不可以把套娃的外层放进内层里 。
63
63
A ab = B (5 );// 然而这个代码可以编译和运行!
64
64
// THINK: 观察打印出的信息,推测把大象放进冰箱分几步?
65
65
// THINK: 这样的代码是“安全”的吗?
You can’t perform that action at this time.
0 commit comments