From 735a24cc939acc1f1b983f1abf9f0bc2c0c212d5 Mon Sep 17 00:00:00 2001 From: YdrMaster Date: Thu, 1 Aug 2024 13:46:05 +0800 Subject: [PATCH] update Signed-off-by: YdrMaster --- exercises/15_class_derive/main.cpp | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/exercises/15_class_derive/main.cpp b/exercises/15_class_derive/main.cpp index 5e6c8d73..819ae72f 100644 --- a/exercises/15_class_derive/main.cpp +++ b/exercises/15_class_derive/main.cpp @@ -2,43 +2,45 @@ // READ: 派生类 +static int i = 0; + struct X { int x; X(int x_) : x(x_) { - std::cout << "X(" << x << ')' << std::endl; + std::cout << ++i << ". " << "X(" << x << ')' << std::endl; } X(X const &other) : x(other.x) { - std::cout << "X(X const &) : x(" << x << ')' << std::endl; + std::cout << ++i << ". " << "X(X const &) : x(" << x << ')' << std::endl; } ~X() { - std::cout << "~X(" << x << ')' << std::endl; + std::cout << ++i << ". " << "~X(" << x << ')' << std::endl; } }; struct A { int a; A(int a_) : a(a_) { - std::cout << "A(" << a << ')' << std::endl; + std::cout << ++i << ". " << "A(" << a << ')' << std::endl; } - A(A const &other) : A(other.a) { - std::cout << "A(A const &) : a(" << a << ')' << std::endl; + A(A const &other) : a(other.a) { + std::cout << ++i << ". " << "A(A const &) : a(" << a << ')' << std::endl; } ~A() { - std::cout << "~A(" << a << ')' << std::endl; + std::cout << ++i << ". " << "~A(" << a << ')' << std::endl; } }; struct B : public A { X x; B(int b) : A(1), x(b) { - std::cout << "B(" << a << ", X(" << x.x << "))" << std::endl; + std::cout << ++i << ". " << "B(" << a << ", X(" << x.x << "))" << std::endl; } B(B const &other) : A(other.a), x(other.x) { - std::cout << "B(B const &) : A(" << a << "), x(X(" << x.x << "))" << std::endl; + std::cout << ++i << ". " << "B(B const &) : A(" << a << "), x(X(" << x.x << "))" << std::endl; } ~B() { - std::cout << "~B(" << a << ", X(" << x.x << "))" << std::endl; + std::cout << ++i << ". " << "~B(" << a << ", X(" << x.x << "))" << std::endl; } }; @@ -52,6 +54,7 @@ int main(int argc, char **argv) { static_assert(sizeof(A) == ?, "There is an int in A"); static_assert(sizeof(B) == ?, "B is an A with an X"); + i = 0; std::cout << std::endl << "-------------------------" << std::endl << std::endl; @@ -65,5 +68,10 @@ int main(int argc, char **argv) { // THINK: 这样的代码是“安全”的吗? // NOTICE: 真实场景中不太可能出现这样的代码 + i = 0; + std::cout << std::endl + << "-------------------------" << std::endl + << std::endl; + return 0; }