会让“这个”变成“这个”吗?回想起来,引用而不是指针更好?

发布于 2024-09-07 12:51:36 字数 459 浏览 4 评论 0原文

可能的重复:
为什么“this”是指针而不是引用?

除了历史语言决定之外,C++ 中的 this 是否有任何理由成为指针而不是引用?这感觉有点奇怪,因为例如复制构造函数或赋值运算符都接受“that”的引用,而不是指针。

[老实说,我找不到这个问题,尽管它确实感觉可能是重复的]

Possible Duplicate:
Why ‘this’ is a pointer and not a reference?

Is there any reason for this in C++ to be a pointer rather than a reference other than historical language decision? It feels a tad strange given that e.g. copy constructor or assignment operators both accept reference to "that", not a pointer.

[I honestly couldn't find this question, though it does feel like a possible duplicate]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守不住的情 2024-09-14 12:51:36

来自 Bjarne Stroustrup 的“C++ 的设计和演化”,Addison-Wesley,1994 年,第 39-40 页:

有时,人们会问为什么 this 是一个指针而不是引用,为什么它被称为 this 而不是 self。当 this 通过类引入到 C 中时,该语言没有引用,并且 C++ 借用了 Simula 而不是 Smalltalk 的术语。

From Bjarne Stroustrup's "The Design and Evolution of C++", Addison-Wesley, 1994, pp 39-40:

Sometimes, people ask why this is a pointer rather than a reference and why it is called this rather than self. When this was introduced into C with Classes, the language didn't have references, and C++ borrows its terminology from Simula rather than Smalltalk.

似最初 2024-09-14 12:51:36

References didn't exist in the language when this was created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文