为什么是“这个”?用作非常量,在 C++ 中已弃用;
为什么 this
在 C++ 中被弃用? C++ 中的 this 指针与 Java 中的 this 指针有何不同?
或者是维基百科错了
早期版本的 C++ 允许更改 this 指针;通过做 因此程序员可以更改方法正在处理的对象。 此功能最终被弃用,现在 C++ 中的此功能为 const 。
Why was this
deprecated in C++? How is the this
pointer in C++ different than this
in Java?
Or is Wikipedia just wrong
Early versions of C++ would let the this pointer be changed; by doing
so a programmer could change which object a method was working on.
This feature was eventually deprecated, and now this in C++ is const .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信你的错误在于你如何解释这句话。他们并不是说“此功能已被弃用”。仅重新分配
this
指针的功能已被弃用。I believe your mistake is in how you interpret that line. They're not saying "The this feature was deprecated". Merely the ability to reassign the
this
pointer was deprecated.根据您的编辑,维基百科文章措辞不佳。
this
并未被弃用,只是允许更改this
指针的功能已被弃用。关键字this
仍然存在。来自 Stroustrup 本人:
为什么“this”不是参考?
Based on your edit, the Wikipedia article is poorly worded.
this
is not deprecated, just that the feature to allowthis
pointer to be changed has been deprecated. The keywordthis
still exists.From Stroustrup himself:
Why is "this" not a reference?
你误解了这句话。
不推荐使用的是
this
- 指针,而不是它指向的对象 - 是可变的。this
本身仍然非常活跃,其类型为纯右值 T*
。 (GCC 暂时模拟了这一点,在当前的 GCC 4.7.0 中将this
设为rvalue T* const
。)You misinterpreted the quote.
What's deprecated is
this
— the pointer, not the object it points to — being mutable.this
itself is still very much alive, with the typeprvalue T*
. (GCC simulates this for the time being, by makingthis
arvalue T* const
in current GCC 4.7.0.)