为什么是“这个”?关键字不是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅 Stroustrup 的为什么
this
不是参考< /a>See Stroustrup's Why is
this
not a reference因为引用直到后来才添加到 C++ 中。到那时再想改变已经太晚了。
Because references weren't added to C++ until later. By then it was too late to change it.
(可能不是完整的答案)
总有这样的情况,一个对象需要以“危险”的方式删除自身,
所以它可能必须是一个指针。
M。
(probably not a complete answer)
There is always the situation when an object needs to delete itself with the "dangerous"
So it probably have to be a pointer.
M.