Skip to content

Commit 198adb8

Browse files
committed
book: strengthen motivation for delegating/inheriting constructors
Delegating constructors led with a vague 'simplify the code'; now it explains the concrete problem (duplicated init logic that a private init function can't solve for const/reference members). Inheriting constructors mislabeled boilerplate as 'inefficiency'; reword to the real motivation (tedious per-constructor forwarding).
1 parent f5c82c6 commit 198adb8

2 files changed

Lines changed: 4 additions & 5 deletions

File tree

book/en-us/02-usability.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,8 +1005,7 @@ SFINAE is powerful but obscure to write and produces verbose error messages. C++
10051005

10061006
*(since C++11)*
10071007

1008-
C++11 introduces the concept of a delegate construct, which allows a constructor to call another constructor
1009-
in a constructor in the same class, thus simplifying the code:
1008+
A class often has several constructors that share a lot of the same initialization logic. Before C++11, the only ways to reuse that logic were to copy it into every constructor, or to extract a private init function — but the latter cannot initialize `const` members or reference members. C++11's **delegating constructors** let one constructor delegate its initialization to another constructor of the same class, removing that duplication:
10101009

10111010
```cpp
10121011
#include <iostream>
@@ -1033,7 +1032,7 @@ int main() {
10331032
10341033
*(since C++11)*
10351034
1036-
In traditional C++, constructors need to pass arguments one by one if they need inheritance, which leads to inefficiency. C++11 introduces the concept of inheritance constructors using the keyword using:
1035+
In traditional C++, if a derived class wants to reuse its base class's constructors, it must redeclare each of them and forward the arguments one by one to the base, which is tedious and error-prone. C++11 introduces inheriting constructors via the `using` keyword, letting a derived class inherit all of the base class's constructors at once:
10371036
10381037
```cpp
10391038
#include <iostream>

book/zh-cn/02-usability.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ SFINAE 虽然强大,但写法晦涩、错误信息冗长。C++20 的 [概念 (
920920

921921
*(C++11)*
922922

923-
C++11 引入了委托构造的概念,这使得构造函数可以在同一个类中一个构造函数调用另一个构造函数,从而达到简化代码的目的
923+
一个类往往有多个构造函数,它们之间常常包含大量重复的初始化逻辑。在 C++11 之前,要复用这些逻辑只能把代码复制进每个构造函数,或抽取一个私有的初始化函数——但后者无法用来初始化 `const` 成员和引用成员。C++11 引入的**委托构造**允许一个构造函数把初始化工作「委托」给同一个类中的另一个构造函数,从而消除这种重复
924924

925925
```cpp
926926
#include <iostream>
@@ -947,7 +947,7 @@ int main() {
947947
948948
*(C++11)*
949949
950-
在传统 C++ 中,构造函数如果需要继承是需要将参数一一传递的,这将导致效率低下。C++11 利用关键字 `using` 引入了继承构造函数的概念:
950+
在传统 C++ 中,如果派生类想要复用基类的构造函数,必须在派生类里逐一重新声明、并把参数一一转发给基类,十分繁琐。C++11 利用关键字 `using` 引入了继承构造函数的概念,让派生类可以「一键」继承基类的全部构造函数
951951
952952
```cpp
953953
#include <iostream>

0 commit comments

Comments
 (0)