为什么是“这个”?关键字不是 C++ 中的引用类型

发布于 2024-08-21 13:50:12 字数 585 浏览 3 评论 0原文

可能的重复:
为什么“this”是指针而不是引用?
SAFE 指向指针的指针(井参考参考)在 C# 中

this 关键字获取指向当前对象的指针。

我的问题是为什么 this 的类型是指针类型而不是引用类型。 是否存在任何条件下 this 关键字将为 NULL 的情况?

我立即想到的是静态函数,但 Visual C++ 至少足够聪明,可以发现这一点并报告静态成员函数没有“this”指针。标准中有这个吗?

Possible Duplicates:
Why ‘this’ is a pointer and not a reference?
SAFE Pointer to a pointer (well reference to a reference) in C#

The this keyword in C++ gets a pointer to the object I currently am.

My question is why is the type of this a pointer type and not a reference type.
Are there any conditions under which the this keyword would be NULL?

My immediate thought would be in a static function, but Visual C++ at least is smart enough to spot this and report static member functions do not have 'this' pointers. Is this in the standard?

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

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

发布评论

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

评论(3

初雪 2024-08-28 13:50:12

请参阅 Stroustrup 的为什么this不是参考< /a>

因为“this”是在添加引用之前引入到 C++ 中的(实际上是带类的 C)。另外,我选择“this”是为了遵循 Simula 的用法,而不是(后来)Smalltalk 使用的“self”。

See Stroustrup's Why is this not a reference

Because "this" was introduced into C++ (really into C with Classes) before references were added. Also, I chose "this" to follow Simula usage, rather than the (later) Smalltalk use of "self".

千紇 2024-08-28 13:50:12

因为引用直到后来才添加到 C++ 中。到那时再想改变已经太晚了。

Because references weren't added to C++ until later. By then it was too late to change it.

朦胧时间 2024-08-28 13:50:12

(可能不是完整的答案)
总有这样的情况,一个对象需要以“危险”的方式删除自身,

delete this;

所以它可能必须是一个指针。

M。

(probably not a complete answer)
There is always the situation when an object needs to delete itself with the "dangerous"

delete this;

So it probably have to be a pointer.

M.

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