Skip to content

Commit

Permalink
删除 CRTP的原理与使用.md 的行尾空格
Browse files Browse the repository at this point in the history
  • Loading branch information
frederick-vs-ja authored Aug 25, 2024
1 parent 60ea15b commit 6a77054
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/卢瑟日经/CRTP的原理与使用.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class X : public Base<X> {
C++23 引入了**显式对象形参**,让我们的 `CRTP` 的形式也出现了变化:
> [显式对象形参](https://zh.cppreference.com/w/cpp/language/member_functions#.E6.98.BE.E5.BC.8F.E5.AF.B9.E8.B1.A1.E6.88.90.E5.91.98.E5.87.BD.E6.95.B0),顾名思义,就是将 C++23 之前,隐式的,由编译器自动将 `this` 指针传递给成员函数使用的,改成**允许用户显式写明**了,也就是:
>
>
> ```cpp
> struct X{
> void f(this const X& self){}
Expand Down Expand Up @@ -113,15 +113,15 @@ d2.name();
上一节我们详细的介绍和解释了 CRTP 的编写范式和原理。现在我们来稍微介绍一下 CRTP 的众多好处。

1. **静态多态**

CRTP 实现静态多态,无需使用虚函数,静态绑定,无运行时开销。

2. **类型安全**

CRTP 提供了类型安全的多态性。通过模板参数传递具体的子类类型,编译器能够确保类型匹配,避免了传统向下转换可能引发的类型错误。

3. **灵活的接口设计**

CRTP 允许父类定义公共接口,并要求子类实现具体的操作。这使得基类能够提供通用的接口,而具体的实现细节留给派生类。其实也就是说多态了。

## 总结
Expand Down

0 comments on commit 6a77054

Please sign in to comment.