The use of this pointer in C + +

Source: Internet
Author: User

1. The use of this pointer :

this pointer is not part of the object itself and does not affect this scope is inside a class, when a non-static member of a class is accessed in a non-static member function of the class. The compiler automatically passes the address of the object itself as an implied parameter to the function. That is, even if you do not write on this pointer, compiler at compile time also add this , which acts as an implicit parameter to a non-static member function, accesses each member through this .   for example, call , this Helped complete this conversion  .

2. Use of this pointer :

One case is to use return *thiswhen returning the class object itself in a non-static member function of the class, and in the other case when the parameter is the same as the member variable name, such as this->n = n (cannot be written as n = n ).

3. This pointer program example :

The this pointer exists in the member function of the class and points to the address of the class instance where the called function resides. Describe the this pointer according to the following procedure  


class Point { int x, y;      


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;} <= "font=" ">  


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 prototype of the Movepoint function should be void movepoint (point *this, int a, int b), and the first argument is a pointer to the class object, which we do not see when defining the member function because the parameter is implied in the class. The Point1 address is passed to this, so it is explicitly written in the Movepoint function:

void Movepoint (int a, int b) {this->x +=a; this-> y+= b;}   That is, you know, Point1 calls the function, that is, the POINT1 data member is called and the value is updated. That is, the function process can be written as point1.x+= A; Point1. Y + = b;

4. A classic answer to this pointer :

When you enter a house,

You can see tables, chairs, floors, etc.

But you can't see the whole picture in the house.

For an instance of a class,

You can see its member functions, the member variables,

But what about the instance itself?

This is a pointer that always points to you as the instance itself

5. The This pointer of the class has the following characteristics:

( 1 ) This can only be used in member functions.

Neither the global function nor the static function can use this.

In fact, the member function defaults to the first parameter is T * const this.

Such as:

Class A



int func (int p)




among them, func The prototype should look like this in the compiler:

int func (A * const this,int P);

( 2 ) Thus, This constructed before the start of a member function, cleared after the end of the member function.

This life cycle is the same as the parameters of any function, without any 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.func (10);

Here, the compiler compiles to:

A::func (&a,10);

It doesn't look like a static function, does it? However, the difference is still there. The compiler usually makes some optimizations to the this pointer, so the this pointer is more efficient to pass--such as the VC usually passes the this parameter through the ECX register.

( 3 ) Several This the problem of the easy mixing of pointers.

A. When was the this pointer created?

This is constructed before the start of the member function, and is cleared after the execution of the member is completed.

But if there are no methods in class or struct, they are not constructors and can only be used as struct C. The value of this pointer is the address of this memory when it is defined in the same way as the type XX, which allocates memory in the stack. When you create an object in new mode, the memory is allocated in the heap, and the new operator returns the assigned address via EAX and sets it to the pointer variable. Then go to call the constructor (if there is a constructor), then the address of the memory block is passed to ECX, then the constructor inside what to do, please see the above answer.

B. Where is the this pointer stored? Heap, stack, global variable, or something else?

The this pointer has different placement locations depending on the compiler. It could be a stack, maybe a register, or even a global variable. At the assembly level, a value appears only in 3 forms: immediate count, register value, and memory variable value. Not stored in registers is stored in memory, they are not associated with high-level language variables.

C. How does the this pointer pass a function in a class? binding? Or is this pointer the first parameter of the function parameter? So, how does the this pointer find the "post-class instance function"?

Most compilers pass the this pointer through the ECX register. In fact, this is also a unspoken rule. In general, different compilers will conform to the same rules, otherwise the obj generated by different compilers will not match.

Before call, the compiler will place the corresponding object address in the EAX. This is passed through the first argument of the function parameter. This pointer is generated before the call, and as for the "post-class instance function", 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.

D. How does the this pointer access variables in a class?

If it is not a class, but a struct, how do you access the variables in the structure by means of struct pointers? If you understand this, it is easy to understand the problem.

in the C + + in  , There is only one difference between classes and structs: The members of a class are by default Private , while the structure is Public .

This is a pointer to a class, if replaced by a structure, that This is the pointer to the structure.

E. We can only use the this pointer through an object after we have obtained an object . If we know the position of an object this pointer, can we use it directly?

The this pointer is defined only in the member function. Therefore, after you obtain an object, you cannot use the this pointer through the object. Therefore, we cannot know the position of the this pointer of an object (only the position of this pointer in the member function). Of course, in the member function, you can know the position of the this pointer (can be obtained through &this), or you can use it directly.

F. after each class is compiled, does the function table in a class be created to hold the function pointer so that it can be used to invoke the function?

A normal class function (either a member function or a static function) does not create a function table to hold the function pointer. Only virtual functions are placed in the function table. However, even virtual functions, if the compiler can clearly know which function to call, the compiler will not be called indirectly through a pointer in the function table, but will call the function directly.

The use of this pointer in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.