A Question Regarding "this"

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
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
closed account (zb0S216C)
Ah, OK. I thought it was because of backwards compatibility with C.

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