Skip to content

修改 cpp_lifetime.md 中的措辞与问题 #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/cpp_lifetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void func() {
```cpp
void func() {
Class *p = new Class; // *p 是动态存储周期
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那这里是否也要强调分配了内存并调用构造函数?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处你没说申请内存,我就没管,认为你只是想强调 *p 是动态存储期。看你心情。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加上吧,多说不算错。

delete p; // 释放动态分配的内存
delete p; // 调用 Class 析构函数并释放动态分配的内存
}
```

Expand All @@ -84,7 +84,9 @@ void func() {

> 特别注意,`p` 依然是“栈上变量”,`p` 指向的 `*p` 才是“堆上变量”!

> 用律师语再说一遍:`p` 是自动存储周期,`p` 指向的 `*p` 才是动态存储周期!(白律师最满意的一集)
> 用律师语再说一遍:
> `p` 只是函数局部对象,拥有自动存储期。它指向的对象才是动态存储期。
> 指针对象本身与指针对象所指向的对象,彼此之间并无任何关系。你可以说 `p` 指向 `*p`,因为 `*p` 表达式返回了指针所指向对象的引用。“所谓*指向*”,无非是指针对象存储了另一个对象的地址

指针本身,和指针指向的对象,是两个东西,不要混淆。

Expand Down