The usage and introduction of this pointer in C++

  • 2020-04-02 01:28:11
  • OfStack

This pointer can only be called in a member function of a class and represents the address of the current object. Here's an example:
 


     void Date::setMonth( int mn ) 
    { 
     month = mn; //These three sentences are equivalent
     this->month = mn; 
     (*this).month = mn; 
    } 
 

1. This can only be used in member functions.
Global and static functions cannot use this.
In fact, the first parameter of the member function defaults to T* const register this.
Such as:
Class A{public: int func(int p){}};
Where, the prototype of func should be: int func(A* const register this, int p);

2. Thus, this is constructed before the start of the member function and cleared after the end of the member.
The life cycle is the same as the parameters of any function.
When a member function of a class is called, the compiler passes a pointer to the class as this argument to the function. Such as:
A, A.
A.f unc (10);
Here, the compiler will compile to: A::func(& A, 10);
Well, it looks just like a static function, right? Still, there are differences. Compilers usually do some optimizations on this pointer, so this pointer is passed more efficiently -- for example, vc is usually passed through the ecx register to pass this parameter.

Answer 3.
#1: when was this pointer created?
This is constructed before the start of the member function and cleared after the completion of the member.

#2: where is this pointer? Heap, stack, global variable, or something else?
This pointer will be placed in different places depending on the compiler. It could be a stack, it could be a register, it could even be a global variable.

#3: how is this passed to a function in a class? The binding? Or the first parameter of the function parameter is this pointer. Then how does this pointer find the function after the class instance?
This is passed by the first argument of the function parameter. This pointer is generated before the call. There is no such thing as a function after an instance of a class. When a class is instantiated, it allocates only the variable space in the class, not the function space. Since the function definition of the class is complete, it's just there, it won't run.

#4: how does this pointer access/of a variable in a class?
If it's not a class, but a structure, how do you access variables in a structure through a structure pointer? If you understand this, you can understand the problem very well.
In C++, there is only one difference between a class and a structure: the members of the class default to private, while the structure is public.
This is a pointer to the class, if replaced by a structure, then this is a pointer to the structure.

#5: only after we get an object, can we use this pointer through the object, if we know the position of this pointer of an object, can we use it directly?
This pointer is defined only in member functions. Therefore, after you get an object, you cannot use this pointer through the object. Therefore, it is impossible to know the position of this pointer to an object. Of course, in the member function, you can know the position of this pointer (you can get &this), or you can use it directly.

#6: after each class is compiled, do you create a function table in the class to hold the function pointer so that you can call the function?
Normal 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, if the compiler knows exactly which function is being called, it will not call it indirectly through a pointer in the function table, but directly.

# 7: how do these compilers do it? 8: can you simulate the implementation?
Knowing the principle, these two problems are easy to understand.
In fact, the mock implementation of this call, in many cases, many people have done.
For example, the system callback function. There are many system callback functions, such as timing, thread and so on.

Take a thread example:


class A{
int n;
public:
static void run(void* pThis){
A* this_ = (A*)pThis;
this_->process();
}
void process(){}
};
main(){
A a;
_beginthread( A::run, 0, &a );
}

Here we define a static function to simulate the member function.

There are also many programs written in C language that simulate the implementation of classes. Such as freetype library and so on.
In fact, most of the people who have used the C language have simulated it. It was just that there was no clear concept.
Such as:


typedef struct student{
int age;
int no;
int scores;
}Student;
void initStudent(Student* pstudent);
void addScore(Student* pstudent, int score);
...

If you change pstudent to this, it's the same thing.

It is equivalent to:


class Student{
public:
int age; int no; int scores;
void initStudent();
void addScore(int score);
}

The const constant can have physical storage space and is therefore addressable

///this pointer is created before the object is created.
This pointer is placed on the stack and is determined at compile time.
And when an object is created, and the entire program runs, there is only one this pointer.


Related articles: