This pointer in class definition

Source: Internet
Author: User
This keyword is often used in the definition of a class, so we can understand the role of this pointer in class definition. Global and static functions cannot use this. In fact, the first parameter of the member function is T * constthis by default. This life cycle is the same as the parameters of any function, and there is no difference. When a class member function is called, the compiler passes the class pointer as the this parameter of the function.

This keyword is often used in the definition of a class, so we can understand the role of this pointer in class definition.

This can only be used in member functions

Global and static functions cannot use this. In fact, the first parameter of the member function is T * const this by default. For example:

class A{public:    int func(int p) {}};

The prototype of func should be:

int func(A* const this, int p);
This is constructed before the start of the member function and cleared after the end of the member function.

This life cycle is the same as the parameters of any function, and there is no difference.

When a class member function is called, the compiler passes the class pointer as the this parameter of the function. For example:

A a;a.func(10);

Here, the compiler will compile it:

A::func(&a, 10);

It looks no different from a static function, right? However, there are still some differences. 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.

Several easily mixed problems with this pointer
  • When was this pointer created?
  • This is constructed before the execution of the member function and cleared after the execution of the member.

    However, if there are no methods in the class or struct, they are not constructor and can only be used as the struct of C. If you use the TYPE xx method, allocate memory in the stack. at this time, the value of this pointer is the address of this memory. If you create an object using the new method, allocate memory in the heap. the new operator returns the allocated address through eax and then sets it to the pointer variable. Then call the constructor (if there is a constructor), then pass the address of the memory block to ecx. then, read the above answer for how to handle the constructor.

  • Where is this pointer stored? Heap, stack, global variable, or others?
  • This pointer has different placement locations for different compilers. It may be a stack, a register, or even a global variable. At the assembly level, a value only appears in three forms: immediate number, register value, and memory variable value. Either in registers or in memory, they do not correspond to advanced language variables.

  • How is this pointer passed to a function in the class? Binding? Or is the first parameter of the function parameter the this pointer? Then, how does the this pointer find the "function after the class instance?
  • Most compilers pass this pointer through the ecx register. In fact, this is also a potential rule. Generally, different compilers follow consistent parameter passing rules. Otherwise, the obj generated by different compilers cannot be matched.

    Before calling, the compiler will put the corresponding object address in eax. This is passed through the first parameter of the function parameter. This pointer is generated before the call. As for "class instance post-function", there is no such statement. When the class is instantiated, the value is allocated variable space in the class, and no space is allocated for the function. Since the function definition of the class is completed, it is there and won't run.

  • How does this pointer refer to variables in the category?
  • If it is not a class but a structure, how can we access the variables in the structure through the structure pointer? If you understand this, it is easy to understand this problem.

    In C ++, there is only one difference between a class and a structure: the class members are private by default, while the structure is public.

    This is a class pointer. if it is replaced by a structure, this is the structure pointer.

  • We can use this pointer through an object only after obtaining an object. If we know the position of an object's this pointer, can we use it directly?
  • This pointer is defined only in member functions. Therefore, after you obtain an object, you cannot use the this pointer through the object. Therefore, we cannot know the position of the this pointer of an object (this pointer is available only in member functions ). Of course, in a Member function, you can know the position of the this pointer (which can be obtained through & this) or directly use it.

  • After each class is compiled, do you want to create a class correspondence table to save the function pointer for calling the function?
  • Ordinary class functions (whether member functions or static functions) do not create a function table to save function pointers. Only virtual functions are placed in the function table.

    However, even if a virtual function is called, if the compiler can clearly determine which function is called, the compiler will not indirectly call the function through the pointer in the function table, but directly call the function.

This article is available at http://www.nowamagic.net/librarys/veda/detail.

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.