C ++ collection -- this pointer, collection -- this pointer
C ++ collection -- this pointer
Preface
When using C ++ for Object-Oriented Programming, this pointer is well known. But do we know about it? Next, we will discuss the meaning and usage of it one by one.
This pointer
Meaning: Why do we need this pointer?
Let's look at 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 too simple! You can understand your fingers at home. We need to think deeply:
1. the setA method clearly does not have this parameter. Why can this be used in the method body?
A: this parameter is implicitly defined. It represents the memory address of the current object. If we do not write it, it does not mean it does not exist.
2. When designing the setA method, I intentionally design the method parameter (int a) and the class member variable (int MyClass: a) to the same name. So that the function body must be written as this-> a = a; to complete the assignment. Can I omit this if I write the form parameters with different names, such as int?
A: Yes. But in essence it is still this-> a = A; or that sentence: we do not write, it does not mean it does not exist. During compilation, the compiler automatically changes "a = A" to "this-> a = ;".
3. Have you figured out the significance of this from the above two questions? Let's talk about the principle of storing programs in memory:The Code area is public and static variables are public.. Public means only one copy.
The former is a public reason: Memory saving. The reason why the latter is public is: logically, this is the case! (Significance of static variables)
In short, any object in the MyClass class calls the same code when calling the setA method. But why is there a different effect? When the object my1 calls setA, it assigns a value to my1.a. When the object my2 calls setA, it assigns a value to my2.a. The code they run is the same, but it won't be messy, because this pointer exists!
Is this all done? No. We need to further point out that the this pointer is implicitly defined in the method parameters. What is its location? I guess this is the first parameter. My reason: When a method is called, The passed real parameters are in the stack from the right to the left. When the stack is output, the parameters at the first position are of course the first out of the stack. (If you have different opinions, please discuss them !) The following discussion is based on this assumption.
Therefore, setA (int a); it is essentially setA (MyClass * const this, int );
My1.setA (2); essentially my1.setA (& my1, 2 );
Further, you will find that I am writing MyClass * const this instead of MyClass * this, and there is another const. Why? Of course there is a reason!
The reason is simple: if this is not a const, you can change the point of this at will, as shown in the following code:
void setA(int a){this = &my;this->a = a;}Of course, such Code cannot be compiled. Every call to setA changes to a value for my..
Summary:By default, this type is a constant pointer to a very popular version of the class type.. (C ++ Primer)
Before we finish, we have to go further ......
What is a const member function? Is it related to this?
We know that const member functions cannot modify member variables. How is it done?
Cause: const's modification to member functions is the modification to this.
In the above example, int getA () const; essentially int getA (MyClass const * const this );
At this point, the truth is finally revealed.
All content directories