views:

1181

answers:

6

I was reading the answers to this question C++ pros and cons and got this doubt while reading the comments. Why the this is a pointer a not a reference ? Any particular reason for making it a pointer?

+40  A: 

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.

Daniel Earwicker
Well, it is also often useful for an object to get a reference to itself. I'd say that's a more common usage. Anyway, the main reason is like you said, references didn't exist when they created the 'this' pointer.
jalf
If 'this' was a reference, the main use of this would probably be 'this.member' instead of 'this->member', so you would not need the ampersand most of the time.
Jonathan Leffler
i completely agree with your first paragraph. and i think its main use is to get the address of the object. but why would that need a pointer? reference will do equally well and will do better for other cases like "this.foo" and "swap(this, bar)"
Johannes Schaub - litb
Good points - I've updated the 2nd paragraph accordingly.
Daniel Earwicker
Also, early on C++ used assignment to this in a constructor in place of having an operator new. That's another important point in its evolution and a reason this is a pointer.
Omnifarious
Omnifarious
conio
@conio - you might want to check that next time you're near a C++ compiler! :) Something like: `int n = 5; int int *p = std::cout << *p;`
Daniel Earwicker
@Daniel: You're right. My bad. :)
conio
@Omnifarious you could write `
Johannes Schaub - litb
+19  A: 

For this and other questions on how C++ got to be the way it is, read Stroustrup's excellent book "The Design & Evolution of C++".

anon
+1 Great advice, anyone interested in how a software product can grow in accordance with feedback from real users should read that book.
Daniel Earwicker
A: 

Please see Earwicker's for the right anwer

rptony
Not only a wild guess but wrong - see Earwickers post for the correct explanation.
anon
i saw the answer and do believe it is right. Appreciate if you could explain what is technically wrong here
rptony
Well, it doesn't make sense. ;)The compiler doesn't track created objects. And even if it did, it would store a pointer to the object, which has nothing to do with 'this' (which only exists when the object is created, and therefore can be initialized)
jalf
+16  A: 

A little late to the party... Straight from the horse's mouth, here's what Stroupstrup 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".

Michael Burr
+2  A: 

Irrespective of how we got here, I think it's lucky that this is a pointer and not a reference as this helps it "make sense" that you can delete it:

void A::f () {
  delete &this;
}

I think this is a case where without necessarily being by design, C++ is better as it is.

Richard Corden
I'd say that's a very good argument for why it should be a reference... Classes are not generally supposed to delete themselves.
jalf
Maybe not generally - but there are examples where you want it to happen. For example an intrusive smart pointer.
Richard Corden
@Richard, In those cases you could just write `delete ` like shown, though :) I think of it like with new-style-casts vs implicit conversions: Make the ugly cases need more characters and make the nicer cases need fewer characters philosophy :)
Johannes Schaub - litb
@litb: True, but my main point was that usually references are assumed to point to a valid objects but with a pointer you need to check. This would be an extremely rare case where a reference was guaranteed to be invalidated.
Richard Corden
A: 

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.


Ayush Goyal

Ayush Goyal