This pointer in C ++

Source: Internet
Author: User

This pointer can only be called in a member function of a class. It represents the address of the current object. The following is an example:

Void Date: setmonth (INT Mn) {month = Mn; // these three sentences are equivalent to this-> month = Mn; (* This). month = Mn ;}

 

1. This can only be used in member functions.
2. Global functions and static functions cannot use this.
3. In fact, the first parameter of the member function is T * const registerthis by default.
For example:
Class A {public: int func (INT p ){}};
In the compiler, the func prototype should be: int func (A * const registerthis, int P );
4. This is constructed before the start of the member function and cleared after the end of the member. This life cycle has the same parameters as any function and has no difference.
5. When a class member function is called, the compiler passes the class pointer as the this parameter of the function.
For example:
A;
A. func (10 );
Here, the compiler will compile it into: A: func (& A, 10 );

6. There is no difference with static functions: the compiler usually optimizes the this pointer. Therefore, the transfer efficiency of this pointer is relatively high-for example, VC usually transmits this parameter through the ECX register.

7. this is constructed before the execution of member functions, and may be registers or even global variables in the member execution stack.
8. this is cleared after the first parameter is passed through the function parameter.
9. this pointer will be placed in different places due to different compilers. It may be.
10. this pointer is created before the object is created. this pointer is placed on the stack and has been determined at the time of compilation.
11. when an object is created and runs throughout the entire program, there is only one 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.