[C + +] this pointer

Source: Internet
Author: User

This pointer

The this pointer of an object is not part of the object itself and does not affect the results of sizeof (the object).

The this pointer is scoped to the inside of the class, and the compiler automatically passes the address of the object itself as an implied parameter to the function when the non-static member function of the class is accessed in the non-static member function of the class.

Give me a chestnut.
struct sales_data {    std::stringconstreturn  bookno;} //   std::string ISBN () const {return this->bookno;}        std::string  bookno;}; int Main () {    sales_data total;    std::string ISBN = total.isbn ();     return 0 ;}

Interpolation: a function defined inside a class is an implicit inline function.

Formally begin to parse this example:

The first is the function call: TOTAL.ISBN ()

We use the dot operator to access the ISBN member of the total object and then call it.

When we call the ISBN member function, we are actually invoking it for an object. If the ISBN points to a member of the Sales_data (such as Bookno). It implicitly points to the member of the object that called the function.

In this example, when ISBN returns Bookno, it actually implicitly returns Total.bookno

The member function accesses the object that called it through an additional implicit parameter called this, and when we call a member function, initialize this with the object address that requested the function.

In this example, the compiler is responsible for passing the address of total to the implicit formal parameter of ISBN in the total.isbn () process. Equivalent to SALES_DATA::ISBN (&total). The address of total is passed in when the ISBN member of the Sales_data is called.

Inside the member function, we can directly use the members of the object that called the function, without having to do this through the member access operator. Because this is exactly what this object refers to. Any direct access to a class member is treated as an implicit reference to this, that is, when ISBN uses bookno, it implicitly uses this to point to the member. Equivalent to This->bookno

The purpose of this is always to point to "this" object, so this is a constant pointer. We are not allowed to change the address stored in this.

[C + +] this pointer

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