2018-12-23

5: 'this' Is Not Any Pointer or Any Variable in C++

<The previous article in this series | The table of contents of this series | The next article in this series>

Why is 'this' a so-called "rvalue"? Because it is not any pointer or any variable.

Topics


About: C++

The table of contents of this article


Starting Context



Target Context


  • The reader will understand that 'this' is not any pointer (or any variable), but a keyword that represents an address.

Orientation


Hypothesizer 7
Most tutorials (whether they are popular or not) casually state that 'this' is a pointer, but then, why does this code cause a compile error like "lvalue required as unary ‘&’ operand"?

@C++ Source Code
#include <iostream>
			
			class Test1 {
				void test1 ();
			};
			
			void Test1::test1 () {
				::std::cout << "The address of 'this' is '" << &this << "'." << ::std::endl << ::std::flush;
			}

"lvalue required"? . . . Huh? . . . If 'this' was a pointer, the expression, "this", should be an lexpression . . .

Let me clarify a thing: 'pointer' is a variable whose value is an address, and any variable used as an expression by itself is an lexpression.

So, there must be a fallacy that has to be rectified, somewhere.


Main Body


1: 'this' Is Not Any Pointer, but a Keyword That Represents an Address


Hypothesizer 7
In fact, 'this' is not any pointer (or any variable), but a keyword that represents an address.

Huh? What is the difference? . . . Well, if someone does not understand the difference, he or she should not understand what 'variable' is.

For example, an expression, "(Test1 * const) 1", is not any pointer (or any variable), but an expression that represents an address, and certainly, is an rexpression.

Although I do not blindly accept what the C++ standard says (because it includes some incongruous explanations), it gives a reasonable explanation on 'this': "the keyword this is a non-lvalue expression whose value is the address of the object for which the function is called".


2: The Conclusion and Beyond


Hypothesizer 7
Now, I seem to understand that 'this' is not any pointer, but a keyword that represents an address.

As there are some other unsatisfactory explanations on C++, I will try to make more reasonable explanations in future articles.


References


<The previous article in this series | The table of contents of this series | The next article in this series>