开发者

Why was "this" used as a non const deprecated in C++

开发者 https://www.devze.com 2023-04-07 10:09 出处:网络
Why was this deprecated in C++? How is the this pointer in C+开发者_如何学Go+ different than this in Java?

Why was this deprecated in C++? How is the this pointer in C+开发者_如何学Go+ 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 .


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.


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".


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号