First, you must understand the meaning of the class. Class should be understood as a type, like int and char, which is a user-defined type. (Although it is much more complicated than the build-in type like int char, you must first understand that they are of the same type ). This type can be used to declare a variable, such as int X and myclass my. In this way, just as variable X has the int type, variable my has the myclass type.
If you understand this, you just need to explain this. In my, this is the pointer to my. If there is another variable myclass Mz, This of MZ is the pointer to MZ. In this way, it is easy to understand that the type of this should be myclass *, and the unreferenced * this should be a variable of the myclass type.
Generally, you need to use the type variable itself when defining the class, because you do not know the variable name at this time (for general purpose, it is impossible to fix the actual variable name ), use a pointer like this to use the variable itself.
1.ThisUsage of pointers:The this pointer of an object is not part of the object and does not affect the result of the sizeof (object. This scope is within the class. When a non-static member of the struct class in the non-static member function of the class, the compiler automatically transmits the address of the object to the function as an implicit parameter. That is to say, even if you do not write the this pointer, the compiler also adds this during compilation. It serves as an implicit parameter of non-static member functions and accesses to all Members are carried out through this.
For example, if you call date. setmonth (9) <==> setmonth (& date, 9), this helps complete this conversion.
2.This
Usage of pointers:One case is that when the class object itself is returned in the non-static member function of the class, return * This is directly used; the other case is that when the parameter and the member variable name are the same, for example, this-> N = n (n = n cannot be written ).
3.
ThisPointer program example:
This pointer exists in a member function of the class and points to the address of the class instance where the called function is located. Use the following program to describe the this pointer
# Include <iostream. h>
Class Point
{
Int X, Y;
Public:
Point (int A, int B) {x = A; y = B ;}
Void movepoint (int A, int B) {x + = A; y + = B ;}
Void print () {cout <"x =" <x <"Y =" <Y <Endl ;}
}; Void main ()
{
Point point1 (10, 10 );
Point1.movepoint (2, 2 );
Point1.print ();
} When the object point1 calls the movepoint (2, 2) function, the address of the point1 object is passed to the this pointer.
The movepoint function is prototype void movepoint (point * This, int A, int B). The first parameter is a pointer to this class object, we didn't see this when defining a member function because this parameter is implicit in the class. In this way, the point1 address is passed to this, so the movepoint function is explicitly written:
Void movepoint (int A, int B) {This-> X + = A; this-> Y + = B ;}
That is, after point1 calls this function, the data member of point1 is called and updated.
That is, this function process can be written as point1.x + = A; point1. y + = B;
4.
AboutThis
A classic answer to the pointer:
When you enter a house,
You can see tables, chairs, floors, etc,
But you cannot see the whole picture of the house. For an instance of a class,
You can see its member functions and member variables,
But what about the instance itself?
This is a pointer that always points to your instance.