Skip to content

Commit

Permalink
fix(exercise): 避免缺少头文件和不完整类型问题
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <[email protected]>
  • Loading branch information
YdrMaster committed Jul 21, 2024
1 parent dba0515 commit 30da850
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 40 deletions.
62 changes: 26 additions & 36 deletions exercises/15_class_derive/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,6 @@

// READ: 派生类 <https://zh.cppreference.com/w/cpp/language/derived_class>

// 三个类型的定义在下方,它们的关系是:B 派生自 A 并包含一个 X 类型的成员。

// ↓↓↓ 这是声明
struct X;
struct A;
struct B;
// ↑↑↑ 这是声明

int main(int argc, char **argv) {
X x = X(1);
A a = A(2);
B b = B(3);

// TODO: 补全三个类型的大小
static_assert(sizeof(X) == ?, "There is an int in X");
static_assert(sizeof(A) == ?, "There is an int in A");
static_assert(sizeof(B) == ?, "B is an A with an X");

std::cout << std::endl
<< "-------------------------" << std::endl
<< std::endl;

// 这是不可能的,A 无法提供 B 增加的成员变量的值
// B ba = A(4);

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

return 0;
}

// ↓↓↓ 这是定义

struct X {
int x;

Expand Down Expand Up @@ -77,3 +41,29 @@ struct B : public A {
std::cout << "~B(" << a << ", X(" << x.x << "))" << std::endl;
}
};

int main(int argc, char **argv) {
X x = X(1);
A a = A(2);
B b = B(3);

// TODO: 补全三个类型的大小
static_assert(sizeof(X) == ?, "There is an int in X");
static_assert(sizeof(A) == ?, "There is an int in A");
static_assert(sizeof(B) == ?, "B is an A with an X");

std::cout << std::endl
<< "-------------------------" << std::endl
<< std::endl;

// 这是不可能的,A 无法提供 B 增加的成员变量的值
// B ba = A(4);

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

return 0;
}
2 changes: 1 addition & 1 deletion exercises/17_class_virtual_destruct/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int main(int argc, char **argv) {

// TODO: 基类指针无法随意转换为派生类指针,补全正确的转换语句
B &bb = *ab;
ASSERT(bb->name() == '?', "Fill in the correct value for bb->name()");
ASSERT(bb.name() == '?', "Fill in the correct value for bb->name()");

// TODO: ---- 以下代码不要修改,通过改正类定义解决编译问题 ----
delete ab;// 通过指针可以删除指向的对象,即使是多态对象
Expand Down
6 changes: 3 additions & 3 deletions exercises/20_class_template/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int main(int argc, char **argv) {
auto t0 = Tensor4D(shape, data);
auto t1 = Tensor4D(shape, data);
t0 += t1;
for (auto i = 0u; i < sizeof(data) / sizeof(int); ++i) {
for (auto i = 0u; i < sizeof(data) / sizeof(*data); ++i) {
ASSERT(t0.data[i] == data[i] * 2, "Tensor doubled by plus its self.");
}
}
Expand Down Expand Up @@ -80,7 +80,7 @@ int main(int argc, char **argv) {
auto t0 = Tensor4D(s0, d0);
auto t1 = Tensor4D(s1, d1);
t0 += t1;
for (auto i = 0u; i < sizeof(d0) / sizeof(int); ++i) {
for (auto i = 0u; i < sizeof(d0) / sizeof(*d0); ++i) {
ASSERT(t0.data[i] == 7.f, "Every element of t0 should be 7 after adding t1 to it.");
}
}
Expand All @@ -102,7 +102,7 @@ int main(int argc, char **argv) {
auto t0 = Tensor4D(s0, d0);
auto t1 = Tensor4D(s1, d1);
t0 += t1;
for (auto i = 0u; i < sizeof(d0) / sizeof(int); ++i) {
for (auto i = 0u; i < sizeof(d0) / sizeof(*d0); ++i) {
ASSERT(t0.data[i] == d0[i] + 1, "Every element of t0 should be incremented by 1 after adding t1 to it.");
}
}
Expand Down
1 change: 1 addition & 0 deletions exercises/21_template_const/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../exercise.h"
#include <cstring>

// READ: 模板非类型实参 <https://zh.cppreference.com/w/cpp/language/template_parameters#%E6%A8%A1%E6%9D%BF%E9%9D%9E%E7%B1%BB%E5%9E%8B%E5%AE%9E%E5%8F%82>

Expand Down
1 change: 1 addition & 0 deletions exercises/23_std_vector/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../exercise.h"
#include <cstring>
#include <vector>

// READ: std::vector <https://zh.cppreference.com/w/cpp/container/vector>
Expand Down

0 comments on commit 30da850

Please sign in to comment.