Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <[email protected]>
  • Loading branch information
YdrMaster committed Jul 28, 2024
1 parent 1e0c19a commit ba51efa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions exercises/12_class_destruct/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../exercise.h"

// READ: 析构函数 <https://zh.cppreference.com/w/cpp/language/destructor>
// READ: RAII <https://learn.microsoft.com/zh-cn/cpp/cpp/object-lifetime-and-resource-management-modern-cpp?view=msvc-170>

/// @brief 任意缓存容量的斐波那契类型。
/// @details 可以在构造时传入缓存容量,因此需要动态分配缓存空间。
Expand Down
4 changes: 3 additions & 1 deletion exercises/13_class_clone/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "../exercise.h"

// READ: 复制构造函数 <https://zh.cppreference.com/w/cpp/language/copy_constructor>
// READ: 函数定义(显式弃置)<https://zh.cppreference.com/w/cpp/language/function>


class DynFibonacci {
size_t *cache;
Expand All @@ -11,7 +13,7 @@ class DynFibonacci {
DynFibonacci(int capacity): cache(new ?), cached(?) {}

// TODO: 实现复制构造器
DynFibonacci(DynFibonacci const &other) = delete;
DynFibonacci(DynFibonacci const &) = delete;

// TODO: 实现析构器,释放缓存空间
~DynFibonacci();
Expand Down
15 changes: 13 additions & 2 deletions exercises/14_class_move/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "../exercise.h"

// READ: 左值右值(概念)<https://learn.microsoft.com/zh-cn/cpp/c-language/l-value-and-r-value-expressions?view=msvc-170>
// READ: 左值右值(细节)<https://zh.cppreference.com/w/cpp/language/value_category>
// READ: 关于移动语义 <https://learn.microsoft.com/zh-cn/cpp/cpp/rvalue-reference-declarator-amp-amp?view=msvc-170#move-semantics>
// READ: 如果实现移动构造 <https://learn.microsoft.com/zh-cn/cpp/cpp/move-constructors-and-move-assignment-operators-cpp?view=msvc-170>

// READ: 移动构造函数 <https://zh.cppreference.com/w/cpp/language/move_constructor>
// READ: 移动赋值 <https://zh.cppreference.com/w/cpp/language/move_assignment>
// READ: 运算符重载 <https://zh.cppreference.com/w/cpp/language/operators>
Expand All @@ -13,11 +18,11 @@ class DynFibonacci {
DynFibonacci(int capacity): cache(new ?), cached(?) {}

// TODO: 实现移动构造器
DynFibonacci(DynFibonacci &&other) noexcept = delete;
DynFibonacci(DynFibonacci &&) noexcept = delete;

// TODO: 实现移动赋值
// NOTICE: ⚠ 注意移动到自身问题 ⚠
DynFibonacci &operator=(DynFibonacci &&other) noexcept = delete;
DynFibonacci &operator=(DynFibonacci &&) noexcept = delete;

// TODO: 实现析构器,释放缓存空间
~DynFibonacci();
Expand All @@ -30,6 +35,12 @@ class DynFibonacci {
return cache[i];
}

// NOTICE: 不要修改这个方法
size_t operator[](int i) const {
ASSERT(i <= cached, "i out of range");
return cache[i];
}

// NOTICE: 不要修改这个方法
bool is_alive() const {
return cache;
Expand Down
2 changes: 1 addition & 1 deletion exercises/15_class_derive/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int main(int argc, char **argv) {
// 这是不可能的,A 无法提供 B 增加的成员变量的值
// B ba = A(4);

// 这也是不可能的,因为 A 是 B 的一部分,就好像不可以把套娃🪆的外层放进内层里
// 这也是不可能的,因为 A 是 B 的一部分,就好像不可以把套娃的外层放进内层里
A ab = B(5);// 然而这个代码可以编译和运行!
// THINK: 观察打印出的信息,推测把大象放进冰箱分几步?
// THINK: 这样的代码是“安全”的吗?
Expand Down

0 comments on commit ba51efa

Please sign in to comment.