Brief analysis of this pointer

Source: Internet
Author: User

Summary

This article is divided into three parts. The first part of the this pointer is caused by the use of the this pointer to distinguish the addresses of different objects from the same function in order for different objects to invoke the same function. The second section describes the use of the this pointer, which requires the this pointer to help implement if the return value of the function is the object itself or the input parameter of the function conflicts with the member variable. In the third part, we describe a few common confusing problems about this pointer.

Body

I. Why there is this

Defines an object to which the system allocates storage space and, if the class contains data members and member functions, allocates storage space to the code for the data and functions, respectively. According to normal thinking, if the class defines 2 objects, then the data and functions of the 2 objects should be allocated space respectively. This is not the case, and theC + + compiler system only uses a space to store the public function code, calling this common function code when invoking the member functions of each object . Therefore, each object's storage space is only the storage space occupied by that object's data members, not including the space occupied by the member function code, which is stored outside of the object space. So, the question is, since the member function code called by all objects is only a single point, how does the member function know which object is currently calling itself?

This is the self-referencing pointer that was born. Whenever an object is created, the system initializes the this pointer to the object, that is, the value of the this pointer is the starting address of the object that is currently calling the member function . Each time a member function is called, the system passes the this pointer as an implied argument to the function. When different objects call the same member function, the C + + compiler determines which object's data members should be called based on the object that the this pointer to the member function points to.


Two. Use of this pointer

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


Three. This pointer is easy to confuse several issues

    • the this pointer can only be used in member functions. global functions, static functions cannot use this. The member function defaults to the first parameter of t* const this.

" example "

function in form:

class A

{

...

int func (int p) {...}

...

}

In the compiler's opinion

Class A

{

...

int func (A *const this,int p) {...}

...

}

    • 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 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.

Cases

function in the form:

...

A;

A.func (10);

...

in compiler view

a A;

a.func (&a);

...

    • The This pointer does not occupy the object's memory space. There is no inclusion relationship between him and the object, except that when the object invokes the function, the object is pointed to by this.
    • Where is this pointer stored? Heap, stack, global variable, or something else?

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.

    • How is this pointer passed to the function in the class? Binding? Or the first parameter of the function parameter is the this pointer, then how to find the "class instance function"???????

This is passed through the first argument of the function. The this pointer is generated before the call.

The function after the class instance, there is no such argument. When a class is instantiated, only the variable space in the class is allocated, and no function definition for the function is assigned to the space class, it is there, it will not run.

    • How does the this pointer access variables in a class? If it's not a class, it's a struct, so how do you access the variables in the structure through a struct pointer?

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, and if it is a struct, this is the pointer to the struct.

    • We only get an object after we can use the this pointer through the object, if we know an object the position of this pointer can be used 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 also 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 &this), can also be used directly.

    • After each class is compiled, do you want to create a class in which the function table holds 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 virtual functions, if the compiler can clearly know which function is called, the compiler will not be called indirectly through a pointer in the function table, but will call the function directly


Brief analysis of this pointer

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: info-contact@alibabacloud.com 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.