9

Possible Duplicate:
Why ‘this’ is a pointer and not a reference?

Is there a good reason that this is a pointer instead of a reference in C++?

peterh
  • 11,875
  • 18
  • 85
  • 108
zneak
  • 134,922
  • 42
  • 253
  • 328

3 Answers3

15

The this concept was introduced before the reference concept was. At the time, this had to be a pointer.Source

GManNickG
  • 494,350
  • 52
  • 494
  • 543
2

From Stroustrup's FAQ

http://www2.research.att.com/~bs/bs_faq2.html#this

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
-2

As crazy as it sounds it is actually legal to delete this;. Sometimes it is even warranted, but very rarely.

Dima
  • 38,860
  • 14
  • 75
  • 115
  • 2
    That doesn't explain why it's a pointer. (Unless you're implying the rationale was that `delete this` needs to be legal.) – GManNickG Aug 24 '10 at 20:42
  • 1
    You could still do `delete &this`. – zneak Aug 24 '10 at 20:44
  • @GMan: You are obviously right, this is not the rationale. But I believe this is relevant to the question nonetheless. – Dima Aug 24 '10 at 20:45
  • @zneak: that would be even more weird than `delete this`. :) – Dima Aug 24 '10 at 20:47
  • 3
    It's relevant in that both the question and answer talk about `this`. But that's about it, I don't see how it has anything to do with the content of the question. :S – GManNickG Aug 24 '10 at 20:47
  • It is relevant, because the next logical question is "What does `this` being a pointer allow me to do?" – Dima Aug 24 '10 at 20:50
  • @Dima: Uh, not really. Then every answer is relevant to any question via some chain of "but then you could ask". You gives answers to asked questions, not answers to unasked/secondary/guessed/imaginary questions. Read it out loud: "Why is `this` a pointer? You can `delete this`!" So? Doesn't answer the question. – GManNickG Aug 24 '10 at 20:52
  • @GMan: Please see the answer by Richard Corden to the original question (http://stackoverflow.com/questions/645994/why-this-is-a-pointer-and-not-a-reference). Apparently, I am not the only one who thinks this is a relevant point. – Dima Aug 24 '10 at 21:02
  • @Dima: Pointing out someone has done the same thing is not, in any way, a rebuttal to an argument. A million people saying X doesn't make X correct, so especially one person doesn't. – GManNickG Aug 24 '10 at 21:09
  • We are not talking about correctness here, since everything I said in my answer is true. We are talking about the relevance to the question, which is inherently a vague notion and is a matter of opinion. I believe it is perfectly reasonable and useful to go a little beyond the question. For example, when the question is about dynamically allocated arrays, do you agree that it is perfectly reasonable to suggest to use std::vector instead? – Dima Aug 24 '10 at 21:32
  • @Dima: Correct as in the answer to "is this a valid answer to post", not the content. I never said anything that gave you reason to draw the conclusion that by correct I meant the content of the answer is factual. You implicitly said "it is correct for me to post this answer, because this person did it", and I pointed out that's a fallacy (ad populum, two wrongs don't make a right, sheer irrelevance); in context, correct in this case should pretty clearly mean "relevant". In any case, you can't go beyond a question if you never answered it in the first place. – GManNickG Aug 24 '10 at 21:39
  • You're right, I should have added to point about `delete this` to your answer. ;) – Dima Aug 24 '10 at 21:42