Skip to content

Commit 02a8018

Browse files
committed
code: cpp11: 10 - delegating constructors
Signed-off-by: sunrisepeak <speakshen@163.com>
1 parent 684a732 commit 02a8018

File tree

6 files changed

+220
-4
lines changed

6 files changed

+220
-4
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,38 @@
2626

2727
> 持续更新中
2828
29-
- **book:** [委托构造函数](https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/10-delegating-constructors.md) - 2025/08/01
30-
- **更多 -> [changelog](https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/changelog.md)**
29+
**cpp11 - 10 - 委托构造函数**
30+
31+
- **[code-0](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/cpp11/10-delegating-constructors-0.cpp) / [code-1](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/cpp11/10-delegating-constructors-1.cpp)** - 2025/08/02
32+
- **[book](https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/10-delegating-constructors.md)** - 2025/08/01
33+
34+
**获取最新内容并进入自动检测**
35+
36+
```bash
37+
d2x update
38+
d2x checker delegating-constructors
39+
```
40+
41+
更多 -> [changelog](https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/changelog.md)
3142

3243
---
3344

3445
## 目标
3546

47+
**项目视角**
48+
3649
- **1.** 任何人都可以在网上**免费获取**课程资源
3750
- **2.** 强调通过**动手实践/代码练习**的方式来学习
3851
- **3.** 提供**自动化的代码练习 + 方便的环境搭建**的工具支持
3952
- **4.** 通过社区共享、共建、互助实现良性循环 - **开源 + 论坛**
4053

54+
**使用者视角**
55+
56+
- [掌握] - **现代C++核心的语言特性**及使用场景
57+
- [掌握] - 通过编译器报错信息**定位问题的能力**
58+
- [熟悉] - 通过文档和cppreference解决C++中不熟悉问题的能力
59+
- [了解] - 如何参与技术社区 - 开源项目的使用、提问题、参与讨论或贡献的姿势
60+
4161
## 交互式代码练习
4262

4363
- 🌍练习进度/状态显示

book/src/changelog.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,14 @@
22

33
## 2025/08
44

5-
- **book:** [委托构造函数](https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/10-delegating-constructors.md) - 2025/08/01
5+
---
6+
**C++11 - 10 - 委托构造函数**
7+
8+
- **[code-0](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/cpp11/10-delegating-constructors-0.cpp) / [code-1](https://github.com/Sunrisepeak/mcpp-standard/blob/main/dslings/cpp11/10-delegating-constructors-1.cpp)** - 2025/08/02
9+
- **[book](https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/10-delegating-constructors.md)** - 2025/08/01
10+
11+
**练习检测命令**
12+
13+
```bash
14+
d2x checker delegating-constructors
15+
```
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2+
// license: Apache-2.0
3+
// file: dslings/cpp11/10-delegating-constructors-0.cpp
4+
//
5+
// Exercise/练习: cpp11 | 10 - delegating constructors | 委托构造函数
6+
//
7+
// Tips/提示: 根据编译器的输出, 修复编译器报错, 了解委托构造函数的基本使用
8+
//
9+
// Docs/文档:
10+
// - https://en.cppreference.com/w/cpp/language/initializer_list.html#Delegating_constructor
11+
// - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/10-delegating-constructors.md
12+
//
13+
// Auto-Checker/自动检测命令:
14+
//
15+
// d2x checker delegating-constructors
16+
//
17+
18+
#include <d2x/common.hpp>
19+
20+
#include <iostream>
21+
#include <string>
22+
23+
static int construction_counter { 0 };
24+
25+
class Account {
26+
std::string id;
27+
std::string name;
28+
std::string coin;
29+
public:
30+
31+
Account(std::string id_) {
32+
id = id_;
33+
name = "momo";
34+
coin = "0元";
35+
36+
D2X_DONT_DELETE_THIS(construction_counter++);
37+
}
38+
39+
Account(std::string id_, std::string name_) {
40+
id = id_;
41+
name = name_;
42+
coin = "0元";
43+
44+
D2X_DONT_DELETE_THIS(construction_counter++);
45+
}
46+
47+
Account(std::string id_, std::string name_, int coin_) {
48+
id = id_;
49+
name = name_;
50+
coin = std::to_string(coin_) + "";
51+
52+
D2X_DONT_DELETE_THIS(construction_counter++);
53+
}
54+
55+
std::string to_string() const {
56+
return "Account { id: " + id + ", name: " + name + ", coin: " + coin + " }";
57+
}
58+
};
59+
60+
int main() { // 不要修改main函数中的代码
61+
62+
Account a1 { "1111" };
63+
d2x_assert_eq(construction_counter, 3);
64+
std::cout << a1.to_string() << std::endl;
65+
66+
Account a2 { "2222", "wukong" };
67+
d2x_assert_eq(construction_counter, 5);
68+
std::cout << a2.to_string() << std::endl;
69+
70+
Account a3 { "3333", "mcpp", 100 };
71+
d2x_assert_eq(construction_counter, 6);
72+
std::cout << a3.to_string() << std::endl;
73+
74+
Account gi { "0000", "GImpact", 648 };
75+
std::cout << gi.to_string() << std::endl;
76+
77+
d2x_assert(
78+
gi.to_string() ==
79+
"Account { id: 0000, name: GImpact, coin: 648原石 }"
80+
);
81+
82+
D2X_WAIT
83+
84+
return 0;
85+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// mcpp-standard: https://github.com/Sunrisepeak/mcpp-standard
2+
// license: Apache-2.0
3+
// file: dslings/cpp11/10-delegating-constructors-1.cpp
4+
//
5+
// Exercise/练习: cpp11 | 10 - delegating constructors | 委托构造函数注意事项
6+
//
7+
// Tips/提示: 根据编译器的输出, 修复编译器报错, 了解委托构造函数的注意事项
8+
//
9+
// Docs/文档:
10+
// - https://en.cppreference.com/w/cpp/language/initializer_list.html#Delegating_constructor
11+
// - https://github.com/Sunrisepeak/mcpp-standard/blob/main/book/src/cpp11/10-delegating-constructors.md
12+
//
13+
// Auto-Checker/自动检测命令:
14+
//
15+
// d2x checker delegating-constructors
16+
//
17+
18+
#include <d2x/common.hpp>
19+
20+
#include <iostream>
21+
#include <string>
22+
23+
struct Object { // 不要修改这个类的代码
24+
static int construction_counter;
25+
std::string name;
26+
Object() {
27+
construction_counter++;
28+
}
29+
30+
Object(std::string name_) : name { name_ } {
31+
construction_counter++;
32+
}
33+
};
34+
35+
class Account {
36+
std::string id;
37+
std::string name;
38+
std::string coin;
39+
Object obj;
40+
public:
41+
42+
Account(std::string id_)
43+
: Account(id_, "momo"), coin { "100元" }
44+
{
45+
46+
}
47+
48+
Account(std::string id_, std::string name_) {
49+
Account(id_, name_, 0);
50+
}
51+
52+
Account(std::string id_, std::string name_, int coin_) {
53+
id = id_;
54+
name = name_;
55+
coin = std::to_string(coin_) + "";
56+
obj = Object(name_);
57+
}
58+
59+
std::string get_id() const {
60+
return id;
61+
}
62+
63+
std::string get_object_name() const {
64+
return obj.name;
65+
}
66+
67+
std::string to_string() const {
68+
return "Account { id: " + id + ", name: " + name + ", coin: " + coin
69+
+ ", Object { name: " + obj.name
70+
+ ", construction_counter: " + std::to_string(Object::construction_counter) + " } }";
71+
}
72+
};
73+
74+
int Object::construction_counter { 0 };
75+
76+
int main() { // 不要修改main函数中的代码
77+
78+
Account a1 { "1111", "hello" };
79+
std::cout << a1.to_string() << std::endl;
80+
d2x_assert(a1.get_id() == "1111");
81+
82+
Object::construction_counter = 0;
83+
Account a2 { "2222", "d2learn", 100 };
84+
std::cout << a2.to_string() << std::endl;
85+
86+
d2x_assert(a2.get_object_name() == "d2learn");
87+
d2x_assert_eq(Object::construction_counter, 1);
88+
89+
D2X_WAIT
90+
91+
return 0;
92+
}

dslings/cpp11/xmake.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,12 @@ target("cpp11-09-list-initialization-2")
107107
add_files("09-list-initialization-2.cpp")
108108

109109
target("cpp11-09-list-initialization-3")
110-
add_files("09-list-initialization-3.cpp")
110+
add_files("09-list-initialization-3.cpp")
111+
112+
-- target: cpp11-10-delegating-constructors
113+
114+
target("cpp11-10-delegating-constructors-0")
115+
add_files("10-delegating-constructors-0.cpp")
116+
117+
target("cpp11-10-delegating-constructors-1")
118+
add_files("10-delegating-constructors-1.cpp")

dslings/d2x/common.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#define D2X_WAIT HONLY_LOGW("🥳 Delete the D2X_WAIT to continue...");
3030
#define D2X_YOUR_ANSWER
31+
#define D2X_DONT_DELETE_THIS(x) x
3132

3233
template<typename F, typename... Args>
3334
class d2x_is_invocable {

0 commit comments

Comments
 (0)