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