C + + supplements--this pointers
Preface
When using C + + for object-oriented programming, this pointer is something that everyone knows. But do we really know about it? Here we discuss the significance of its existence and how to use it.
This pointer
Existence Meaning: Why do we need this pointer?
To see a simple example
#include <iostream>using namespace Std;class myclass{protected:int a;public:myclass (int a): A (a) {}void SetA (int a {this->a = A;} int Geta () const{return A;}; int main () {MyClass my1 (0); MyClass my2 (1); My1.seta (2); My2.seta (3); cout << "my1::a =" << My1.geta () << endl;cout << "my2::a = "<< My2.geta () << endl;cin.get (); return 0;}
Run
This example is simply too simple! Everyone can see with their toes. We need to think deeply about:
There is no this parameter in the 1.setA method, why can this be used in the method body?
A: The This parameter is implicitly defined, which represents the memory address of the current object. We do not write, does not mean that it does not exist.
2. When designing the Seta method, I deliberately designed the method parameter (int a) and the class member variable (int myclass::a) to have the same name. So that the function body must be written this->a=a; to complete the assignment. If the formal parameter is written in a different name, such as int A, can I omit this?
Answer: Yes. But in essence it is this->a=a; or the sentence: We do not write, does not mean that it does not exist. When compiling, the compiler automatically changes "A=a" to "this->a=a;".
3. From the above two questions have not yet clarified the existence of this meaning? That said, the program in memory storage principle: code area is public, static variables are public . There is only one part of the public meaning.
The former is the public reason: save memory. The latter is the public reason: logically it should be so! (The existence meaning of static variables)
In summary, any object of class MyClass calls the same code when calling the Seta method. But why do they have different effects? The My1 object is assigned to my1.a when it calls Seta. The My2 object is assigned to my2.a when it calls Seta. They execute the same code, but they don't mess up because the this pointer exists!
Is that it? Not yet, we need to point out further: the this pointer is implicitly defined in the method parameter. So where is it? This is not a good thing to say, my guess: the first parameter. My reasons: When the method call, the arguments passed in from right to left into the stack, when the stack, the first position of course the parameter is the first out of the stack. (If you have a different view, welcome to the discussion!) The following discussion is based on this hypothesis.
So, SetA (int a); Essentially, it should be SetA (MyClass * const this, int a);
My1.seta (2); essentially My1.seta (&my1, 2);
Further, careful you will find that I write MyClass * const This, not MyClass * This, more a const, why? There's a reason for that, of course!
The reason is simple: if this is not const, then you can change the direction of this, as in the following code:
void SetA (int a) {this = &my;this->a = A;}
Of course, such code must not be compiled. Each call to Seta becomes an assignment to the my.a.
Summary: By default, the type of this is a constant pointer to a very version of the class type . ("C++primer")
Not yet, we have to go further ...
What is the const member function? Does it have a connection with this?
We know that the Const member function is not allowed to modify member variables. And how did it do it?
Cause: The modification of a const member function is the modification of this.
The above example is an int geta () const, which is essentially an int geta (MyClass const * const);
At this point, the truth is finally revealed.
C + + supplements--this pointers