The this pointer can only be called in a member function of a class, which represents the address of the current object. Here's a sample:voidDate::setmonth (intmn) {Month=mn;//These three sentences are equivalent. This -Month=mn; (* This). Month=mn; } 1. This can only be used in member functions. Global functions, static functions cannot use this. In fact, the member function defaults to the first number of parameters T* ConstRegister This. such as:classa{ Public: intfunc (intp) {}}; in which the prototype of Func should look like this:intfunc (A* ConstRegister This, intp); 2This shows that this is constructed before the start of the member function and is cleared after the member's end. This life cycle is the same as any function, no matter what the difference. When a member function of a class is called, the compiler passes the pointer of the class as the This parameter of the function. such as: A A;a.func (Ten); Here, the compiler compiles: A::func (&A,TenWell, it doesn't look like a static function, does it? Just, the difference is still there. The compiler generally makes some optimizations for the this pointer, so the this pointer is more efficient to pass--for example, a VC typically passes the This parameter through the ECX register. 3. Answer #1: When was the this pointer created??This is constructed before the start of the member function and is cleared after the member's run is complete. #2: Where is this pointer stored?heap, stack, global variable, or other?the this pointer differs depending on the compiler and where it is placed. It could be a stack, maybe a register, or even a global variable. #3: How the This pointer is passed to the function in the class?binding?or the first parameter in the function parameter is the this pointer. Then how does the this pointer find the function after the class instance?This is passed by the first number of function references. The this pointer is generated before the call. The function after the class instance, there is no such argument. When a class is instantiated, it allocates only the variable space in the class and does not allocate space for the function. Since the function definition of the class is complete, it is there and will not run. #4: This pointer how to access the variables in the class/?Suppose it is not a class, but a structure, how do you access variables in the structure by using structure pointers? If you make this clear, it's a good idea to understand the problem. In C++, classes and structs are only one difference: Members of a class are private by default, and structs are public. This is a pointer to a class, assuming a structure, then this is a pointer to the structure. #5: We only have access to an object after the ability to use the this pointer through the object, suppose we know the position of an object this pointer can be used directly?The this pointer is only defined in the member function. Therefore, after you obtain an object, you cannot use the this pointer through the object. Therefore, we also cannot know the position of the this pointer of an object (there is only the position of this pointer in the member function). Of course, in the member function, you are able to know the position of the this pointer (can&This is obtained) and can also be used directly. #6: After each class is compiled, whether to create a class in the function table to save the function pointer so that it can be used to invoke the function?Ordinary class functions, whether member functions or static functions, do not create a function table to hold function pointers. Only virtual functions are placed in the function table. However, even if it is a virtual function, assuming that the compiler knows which function is being called, the compiler will not invoke it indirectly through pointers in the function table, but will call the function directly. # 7: How do these compilers do that??8: Can you simulate the implementation?Knowing the principle, these two problems are very easy to understand. In fact, the analog implementation of this call, in very many cases, very many people have done. For example, a system callback function. There are a lot of system callback functions, such as timing, threading, etc. Example of a thread:classa{intN; Public:Static voidRun (void*pThis) {A*This_=(A*) Pthis;this_ -process ();}voidprocess () {}};main () {A a;_beginthread (A::run,0, &a);} This is where you define a static function to simulate member functions. There are also many programs written in C that simulate the implementation of classes. such as FreeType library and so on. In fact, most people who have applied C language have simulated it. It was just a concept that was not understood at the time. such as: TypeDefstructstudent{intAge ;intNo;intscores;} Student;voidinitstudent (Student*pstudent);voidAddscore (Student*Pstudent,intscore);.. If you change pstudent to this, it will be the same. It is equivalent to:classstudent{ Public:intAge ;intNo;intscores;voidinitstudent ();voidAddscore (intscore);} Const constants can have physical storage space, so it is possible to take an address///The this pointer is created before the object is created.The this pointer is placed on the stack and is determined at compile time. And when an object is created, there is only one this pointer during execution of the entire program execution.
How the this pointer is used in C + +.