Skip to content
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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Mq-b
Copy link
Contributor

@Mq-b Mq-b commented Dec 20, 2024

Class *p = new Class;  // *p 是动态存储周期
delete p;              // 释放动态分配的内存

delete p; 的注释仅为“释放动态分配的内存”不合适,根据上下文,此处还应强调调用析构函数。

特别注意,p 依然是“栈上变量”,p 指向的 *p 才是“堆上变量”!
用律师语再说一遍:p 是自动存储周期,p 指向的 *p 才是动态存储周期!(白律师最满意的一集)

律师的话并不够准确。且我认为之前的描述也存在一些问题。

我认为关键在于强调 p 是什么,*p 是什么,以及说明常用词汇“指向”到底代指着什么。

另外有必要强调 *p 返回的是指针所指向对象的引用,我认为大多数开发者事实上并不清楚这一点。

内建的取址符(*)的操作数必须是指向对象或指向函数的指针,其结果是一个 lvalue,引用的是操作数所指向的对象或函数。

T& operator*(T*);

@archibate
Copy link
Contributor

不!小彭老师背地里cue白律师被发现了 😭

@@ -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.

加上吧,多说不算错。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants