Why 'this' is a pointer and not a reference?
I was reading the answers to this question C++ pros and cons and got this doubt while reading the comments.
programmers frequently find it confusing that "this" is a pointer but not a reference. another confusion is why "hello" is not of type std::string but evaluates to a char const* (pointer) (after array to pointer conversion) – Johannes Schaub - litb Dec 22 '08 at 1:56
That only shows that it doesn't use the same conventions as other (later) languages. – le dorfier Dec 22 '08 at 3:35
I'd call the "this" thing a pretty trivial issue though. And oops, thanks for catching a few errors in my examples of undefined behavior. :) Although I don't understand what info about size has to do with anything in the first one. A pointer is simply not allowed to point outside allocated memory – jalf Dec 22 '08 at 4:18
Is this a constant poiner? – yesraaj Dec 22 '08 at 6:35
this can be constant if the method is const int getFoo() const; <- in the scope of getFoo, "this" is constant, and is therefore readonly. This prevents bugs and provides some level of guarantee to the caller that the object won't change. – Doug T. Dec 22 '08 at 16:42
you can't reassign "this". ie you cannot do "this = &other;", because this is an rvalue. but this is of type T*, not of type T const . ie it's a non-constant pointer. if you are in a const method, then it's a pointer to const. T const . but the pointer itself is nonconst – Johannes Schaub - litb Dec 22 '08 at 17:53
think of "this" like this: #define this (this_ + 0) where the compiler creates "this_" as a pointer to the object and makes "this" a keyword. you can't assign "this" because (this_ + 0) is an rvalue. of course that's not how it is (there is no such macro), but it can help understand it – Johannes Schaub - litb Dec 22 '08 at 17:55
My question is, why this
is a pointer a not a reference? Any particular reason for making it a pointer?
Some further arguments why this
being a reference would make sense:
Item 1
from More Effective C++
: use references when it is guaranteed that we have a valid object ie not a NULL (my interpretation). .
) is a little bit nicer and shorter than accessing pointers ( ->
or (*)
). When the language was first evolving, in early releases with real users, there were no references, only pointers. References were added when operator overloading was added, as it requires references to work consistently.
One of the uses of this
is for an object to get a pointer to itself. If it was a reference, we'd have to write &this
. On the other hand, when we write an assignment operator we have to return *this
, which would look simpler as return this
. So if you had a blank slate, you could argue it either way. But C++ evolved gradually in response to feedback from a community of users (like most successful things). The value of backward compatibility totally overwhelms the minor advantages/disadvantages stemming from this
being a reference or a pointer.
A little late to the party... Straight from the horse's mouth, here's what Bjarne Stroustrup has to say (which is essentially repeated in or taken from the "Design and Evolution of C++" book):
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".
The C++ standard states that
9.3.2/1
In the body of a nonstatic (9.3) member function, the keyword this is a non-lvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.
But in other references , it was found something else.. so someone took initiative and shot a mail to Mr. Stroustrup. The conversation that followed can be found here .
链接地址: http://www.djcxy.com/p/20782.html上一篇: C ++通过引用将`this`传入方法
下一篇: 为什么'这个'是一个指针而不是一个参考?