为什么是“这个”?用作非常量,在 C++ 中已弃用;

发布于 2024-12-06 07:45:35 字数 326 浏览 2 评论 0原文

为什么 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 技术交流群。

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

发布评论

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

评论(3

暖阳 2024-12-13 07:45:35

我相信你的错误在于你如何解释这句话。他们并不是说“此功能已被弃用”。仅重新分配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.

沙沙粒小 2024-12-13 07:45:35

根据您的编辑,维基百科文章措辞不佳。 this 并未被弃用,只是允许更改 this 指针的功能已被弃用。关键字 this 仍然存在。

来自 Stroustrup 本人

为什么“this”不是参考?

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

Based on your edit, the Wikipedia article is poorly worded. this is not deprecated, just that the feature to allow this pointer to be changed has been deprecated. The keyword this still exists.

From Stroustrup himself:

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-12-13 07:45:35

你误解了这句话。

早期版本的 C++ 允许更改 this 指针;通过这样做,程序员可以更改方法正在处理的对象。此功能最终被弃用,现在 C++ 中的此功能为 const

不推荐使用的是 this - 指针,而不是它指向的对象 - 是可变的。

this 本身仍然非常活跃,其类型为 纯右值 T*。 (GCC 暂时模拟了这一点,在当前的 GCC 4.7.0 中将 this 设为 rvalue T* const。)

You misinterpreted the quote.

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.

What's deprecated is this — the pointer, not the object it points to — being mutable.

this itself is still very much alive, with the type prvalue T*. (GCC simulates this for the time being, by making this a rvalue T* const in current GCC 4.7.0.)

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