C + + constant pointer this

Source: Internet
Author: User

Each member function contains a constant pointer, which we call the this pointer, which points to the object that called the function, whose value is the first address of the object. With this pointer, we can access the object in the function body of the member function. Where this is a key word for C + +.

Example 1:

Thunder member Share

As shown in the example 1 program, this example shows us how to use this pointer. Of course, we do not need this in the actual program design, this is too cumbersome to write, example 1 is to do the introduction of the basic syntax of this pointer. In the actual design of the program only need to follow example 2 on the line.

Example 2:

Usually we design member functions to design the shape participates in the member variable to the same name, as shown in Example 3, when the function is defined by the Price = Price, such a program always seems awkward. The use of this pointer will make it very clear to distinguish two price.

Example 3:

August 5 Thunder Share

Example 4:

Youku member sharing

As shown in Example 4, a this->price is used to indicate that the previous price is a member variable in the class, and the following price is the price in the formal parameter. This pointer avoids ambiguity in the understanding of naming conflicts at this time.

Example 5

Hollywood Account

Iqiyi Art account Sharing

In this example, we have added a copy function for the book class to copy the book class object to the object that called the copy function, in the function body, we use the this pointer to determine whether the reference to the object being copied is the object itself that called the function, and if so, the function is introduced. In the main function harry.copy (Harry), which is copying itself, is of course what we do not want to see, and therefore exits the function directly. This is also a common detection method in the process of programming.

The this pointer is a constant pointer, and its value cannot be modified, and all attempts to modify the pointer, such as assignment, increment, decrement, and so on, are not allowed. In addition, this pointer is only valid in non-static member functions, and after we have introduced the static member function, you will understand why this is so, as long as you know this here.

Example 6:

In Example 6, the this pointer appears in the static member function and the compilation error occurs. The this pointer can only be used within a non-static member function.

C + + constant pointer this

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.