Why 'this' Is a Pointer and Not a Reference
The notion of "this" in C typically evokes confusion, as some perceive it as a pointer while others see it as a reference. Historical context sheds light on this apparent contradiction.
Initially, C featured only pointers. When operator overloading was introduced, references became a necessity to ensure consistency. However, "this" was already established as a pointer in earlier versions.
If "this" were a reference, accessing it would require the "&" operator, resulting in "&this." In contrast, the syntax for self-assignments using pointers, "*this," appears more succinct. Thus, the backwards compatibility of "this" being a pointer outweighs the potential benefits of making it a reference.
While references offer advantages in terms of safety and简洁性, the established convention of "this" being a pointer remains unchanged due to the C community's emphasis on backward compatibility.
The above is the detailed content of Is `this` a Pointer or a Reference in C ?. For more information, please follow other related articles on the PHP Chinese website!