Skip to content

Commit 13f82a5

Browse files
authored
update C overview, rtti.md
1 parent 742d87f commit 13f82a5

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

docs/c-cpp/basics/rtti.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# RTTI - Run Time Type Information
2+
3+
// Run Time Type Information
4+
``` cpp
5+
#include <ios>
6+
#include <iostream>
7+
using namespace std;
8+
class Base
9+
{
10+
public:
11+
Base() { cout << __FUNCTION__ << endl; }
12+
virtual ~Base() { cout << __FUNCTION__ << endl; }
13+
virtual void f()
14+
{
15+
cout << "Base" << __FUNCTION__ << endl;
16+
}
17+
};
18+
class Derived : public Base
19+
{
20+
public:
21+
Derived() { cout << __FUNCTION__ << endl; }
22+
~Derived() { cout << __FUNCTION__ << endl; }
23+
24+
void f()
25+
{
26+
cout << "Derived-" << __FUNCTION__ << endl;
27+
}
28+
};
29+
class Derived2 : public Derived
30+
{
31+
public:
32+
Derived2() { cout << __FUNCTION__ << endl; }
33+
~Derived2() { cout << __FUNCTION__ << endl; }
34+
void f()
35+
{
36+
cout << "Derived2-" << __FUNCTION__ << endl;
37+
}
38+
};
39+
int main(void)
40+
{
41+
Derived* p = new Derived2();
42+
cout << "-------------------" << endl;
43+
p->f();
44+
cout << "-------------------" << endl;
45+
auto p2 = dynamic_cast<Derived2*>(p);
46+
auto p3 = dynamic_cast<Base*>(p2);
47+
cout << sizeof(p) << ", "
48+
<< typeid(p).name() << ", "
49+
<< std::hex << typeid(p).hash_code() << endl;
50+
cout << "p2: " << typeid(p2).name() << ", "
51+
<< std::hex << typeid(p2).hash_code() << endl;
52+
cout << "p3: " << typeid(p3).name() << ", "
53+
<< std::hex << typeid(p3).hash_code() << endl;
54+
cout << sizeof(*p) << ", "
55+
<< typeid(*p).name() << ", "
56+
<< std::hex << typeid(*p).hash_code() << endl;
57+
cout << "-------------------" << endl;
58+
delete p;
59+
cout << "-------------------" << endl;
60+
return 0;
61+
}
62+
```
63+
64+
!!! experiment "Output"
65+
```cpp
66+
Base
67+
Derived
68+
Derived2
69+
-------------------
70+
Derived2-f
71+
-------------------
72+
8, P7Derived, 9a7969d972aa52f7
73+
p2: P8Derived2, 16a9329eaa369fd5
74+
p3: P4Base, 11468d01f070011b
75+
8, 8Derived2, f3ec1aaa6ce2faa5
76+
-------------------
77+
~Derived2
78+
~Derived
79+
~Base
80+
-------------------
81+
```
82+
83+
---

docs/c-cpp/c/00.overview.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,15 @@ C语言诞生于上个世界70年代初期,由Dennis Ritchie在贝尔实验室
1919
他晚年还编写了《Unix传奇》一书。
2020

2121
C语言对于后续的编程语言影响深远,C++、C#、Java、JavaScript、Objective-C等语言都直接或间接地受到了C语言的影响。<br>
22-
K&R TCPL一书中的Hello World程序成为了编程入门的经典示例。<br>
22+
K&R TCPL一书中的Hello World程序成为了编程入门的经典示例。<br>
23+
24+
## 编码规范
25+
26+
我个人比较喜欢[OpenResty的编码规范](https://openresty.org/cn/c-coding-style-guide.html)
27+
28+
## 推荐内容
29+
30+
- [C程序设计语言 K&R TCPL](https://book.douban.com/subject/1139336/)
31+
- [C程序设计语言(中文版)](https://www.learncs.site/assets/files/c-programming-language-2nd-edition-simple-chinese-4ca79b68a8a3542f3e951afe6d1352b1.pdf)
32+
- [浙江大学翁恺教你C语言程序设计](https://www.bilibili.com/video/BV1dr4y1n7vA/?spm_id_from=333.337.search-card.all.click)
33+
- [极客时间 - 深入 C 语言和程序运行原理](https://time.geekbang.org/column/intro/100100701?utm_campaign=geektime_search&utm_content=geektime_search&utm_medium=geektime_search&utm_source=geektime_search&utm_term=geektime_search&tab=catalog)

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ nav:
156156
- virtual: c-cpp/basics/virtual.md
157157
- final: c-cpp/basics/final.md
158158
- Pointer and Reference: c-cpp/basics/pointer_and_ref.md
159+
- RTTI: c-cpp/basics/rtti.md
159160
- Design Pattern:
160161
- Template Method: c-cpp/design_pattern/template_method.md
161162
- Build: c-cpp/build.md

0 commit comments

Comments
 (0)