C++ classes and objects (2) this pointer

Table of contents

1. The concept of this pointer 

Second, the characteristics of this pointer

3. Where does the this pointer exist?

4. Can the this pointer be empty? 

5. Can the this pointer be used in the constructor?


1. The concept of this pointer 

1. What is it?

It is an implicit formal parameter         of a non-static member function in the class , and the this pointer points to the object calling the function.

The this pointer is a hidden parameter         passed by the C++ compiler to each "non-static member function" by default , and the this pointer points to the object that called the member function.


2. Why is there a this pointer?

        How to let the member functions and member variables in the object see the object itself? ——Through the this pointer: this is a pointer, which points to the object itself at all times.


3. How to use it?

        The this pointer can only be called in a member function of a class, and it represents the address of the current object.


Second, the characteristics of this pointer

1. The type of this pointer: the type of class * const, so this pointer is modified by const and cannot be modified .


2. The this pointer is implicit and can only be used inside the "member function" .


3. The this pointer is essentially the formal parameter of the "member function". When the object calls the member function, the object address is passed as the actual parameter to the this formal parameter. Therefore, the this pointer is not stored in the object, that is, the this pointer is not part of the object itself, and will not affect the result of sizeof(object).


4. Member functions actually hide a this pointer parameter, which is passed by the compiler by default and does not need to be passed by us.

eg. Actual member functions and member function calls.

​​

eg.this pointer is passed by the compiler.


3. Where does the this pointer exist?

The this pointer is generally stored on the stack like other formal parameters. It is not in the object, so the this pointer is not calculated when calculating the size of the class.


4. Can the this pointer be empty? 

Yes, for example: when using nullptr to instantiate an object, the this pointer is empty and points to an empty address.

But in this way, it is impossible to use the this pointer to call the members of the class, because the this pointer points to an empty address, and an error will be reported if the members of the class are forcibly called.


5. Can the this pointer be used in the constructor? 

Why do you ask this question?

        Because when using the constructor, you are constructing the object and opening up space; and the this pointer points to the space of the object, so there is a problem of who comes first. If the this pointer gets the object space first, and the object has not yet been constructed, it will error.


Yes, it can be easily remembered: the constructor is also a member function, so it can be used.

Because there are two steps in the constructor: 1. Construct the object and open up space; 2. After entering the function body: assign a value. So when the constructor uses the this pointer, the space has already been opened up, so the this pointer can be used in the constructor.

Guess you like

Origin blog.csdn.net/look_outs/article/details/129151554