A Question Regarding "this"

Sep 22, 2012 at 9:53am
closed account (zb0S216C)
We all know that "this" is always passed an address, and cannot be assigned to null. Based on the rules imposed on the declaration of the "this" parameter, "this" behaves almost like a reference - so why not implement it as a reference? Is there any particular reason why a pointer was chosen?

Wazzak
Sep 22, 2012 at 10:10am
At the time of choosing, references didn't exist. C++ was still being carved out by Stroustrup, his strong Nordic hands tearing and reshaping the very fabric of the (programming) universe, bending it to his will. RRRAAGGHH!
Last edited on Sep 22, 2012 at 10:26am
Sep 22, 2012 at 10:26am
closed account (zb0S216C)
Ah, OK. I thought it was because of backwards compatibility with C.

Wazzak
Sep 22, 2012 at 10:36am
I think that it could have been changed once references existed, but wasn't, to maintain backwards compatibility with the C++ of the day before.

cf VB .NET, Perl 6, PHP 5.something, and probably lots more :)
Last edited on Sep 22, 2012 at 12:41pm
Sep 22, 2012 at 12:35pm
It wouldn't have been backwards compatible with C. Classes are structures in memory that hold class methods. When you call a function that belongs to a class within the class, you are actually referencing the this pointer and then the class method:

this->function();

You have to; otherwise the compiler doesn't know where to look.
Sep 22, 2012 at 2:07pm
closed account (zb0S216C)
@Moschops: That makes sense.

@pogrady: I'm aware how "this" works, but I was curious as to why "this" was implemented the way it was.

Wazzak
Topic archived. No new replies allowed.