Skip to content

Commit

Permalink
docs(exercise): 补充阅读材料和提示
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <ydrml@hotmail.com>
  • Loading branch information
YdrMaster committed Jul 30, 2024
1 parent 1e0c19a commit 56ff1c1
Show file tree
Hide file tree
Showing 5 changed files with 21 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
3 changes: 3 additions & 0 deletions exercises/24_std_vector_bool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ int main(int argc, char **argv) {
ASSERT(vec[0], "Make this assertion pass.");
ASSERT(vec[99], "Make this assertion pass.");
ASSERT(vec.size() == 100, "Make this assertion pass.");
// NOTICE: 平台相关!注意 CI:Ubuntu 上的值。
std::cout << "sizeof(std::vector<bool>) = " << sizeof(std::vector<bool>) << std::endl;
ASSERT(sizeof(vec) == ?, "Fill in the correct value.");
{
vec[20] = false;
Expand All @@ -25,6 +27,7 @@ int main(int argc, char **argv) {
ASSERT(?ref, "Fill in `ref` or `!ref`");
ref = false;
ASSERT(?ref, "Fill in `ref` or `!ref`");
// THINK: WHAT and WHY?
ASSERT(?vec[30], "Fill in `vec[30]` or `!vec[30]`.");
}
return 0;
Expand Down

0 comments on commit 56ff1c1

Please sign in to comment.