Summary of C++this __c++

Source: Internet
Author: User
Tags function definition

Today I'll make a summary of this knowledge point.

1. What is this

To understand the meaning of class first, class should be understood as a type, like Int,char, which is a user-defined type. (although build-in types are much more complex than int char, the first thing to understand

They are the same type). This type can be used to declare a variable, such as int x, MyClass my, and so on. This way, as variable x has an int type, the variable my has a MyClass type. Understand this

, this is a good explanation, my here is the pointer to my. If there is a variable MyClass MZ,MZ This is a pointer to the MZ, the solution to the reference *this should be a

A variable of type MyClass. If a member function is called, such as A.sum (), the SUM function can access a by using the stealth parameter this, so this is the worker whose object is invoked to access the

A tool that can be understood as this points to the object calling him. By default, the type of this is a constant pointer to the very magnitude of the class type, so by default we cannot bind this to the

A constant object, so adding a const modifier to this property to point to a constant object can increase the flexibility of the function, and this member function is also called

The volume member function.

the usefulness of 2.this

The this pointer for an object is not part of the object itself and does not affect the results of the sizeof (object). This scope is within a class and can only be used in member functions when a non-static member of a class is accessed in a Non-static member function of a class, the compiler automatically passes the address of the object itself as a suppressed parameter to the function. That is, even if you do not write the this pointer, the compiler compiles the

Time is also added to this, as a non-static member function of the implied parameters, access to each member through this. For example, tune date. Setmonth (9) <=>setmonth (&date, 9), this

Help complete this conversion.

3. This pointer's common use place

A: When returning the class object itself in the Non-static member function of the class, use return *this directly;

B: The other case is when the parameter is the same as the member variable name, such as This->n = N (cannot write n = n);

C: When you use the type variable itself when you define the class, because you don't know the name of the variable at this time, use a pointer like this to work with the variable itself.

4. Common Problems of confusion

A. this pointer was created when.

This is constructed before the start of the member function, which is cleared after the execution of the member is completed. But if there are no methods in class or struct, they are not constructors, they can only be used as C

struct use. In the case of type XX, the memory is allocated in the stack, at which point the value of this pointer is the address of this block of memory. To create objects in the new way, distribute them in the heap.

Memory, the new operator returns the assigned address via EAX and then sets it to the pointer variable. Then call the constructor (if there is a constructor), and then pass the address of the memory block to the

ECX, what to do with the constructor, look at the answer above.

B. this pointer is stored where. Heap, stack, global variable, or other.

The this pointer will have different placement locations depending on the compiler. It could be a stack, it could be a register, or even a global variable. At the assembly level, a value will only appear in 3 forms: immediate number,

Register value and memory variable value. Not stored in registers or stored in memory, they do not correspond to high-level language variables.

C. this pointer is how to pass a function in a class. Binding. Or the first argument in the function argument is the this pointer. So, how does this pointer find the "function after class instance".

Most compilers pass the this pointer through the ECX register. In fact, this is also a unspoken rule. In general, different compilers follow consistent parameter rules, otherwise the obj generated by different compilers

Can not match it. Before call, the compiler puts the corresponding object address into the EAX. This is passed by the first parameter of the function argument. The this pointer is generated before the call, as to "class instance after letter

Number ", without this claim. When instantiating a class, only the variable space in the class is allocated, and no space is allocated for the function. Since the function definition of a class has been completed, it is there and will not run.

D. This pointer is how to access variables in a class.

If it's not a class, but a struct, then how do you access the variables in the structure through the structure pointer? If you understand this, it is easy to understand the problem. In C + +, classes and

Structs are only one difference: the members of a class are private by default, and the structure is public. This is the pointer to the class, and if you replace it with a struct, this is the pointer to the structure.

E. We can only use the this pointer through an object after we get an object. If we know the position of an object this pointer, you can use it directly.

The this pointer is only defined in member functions. Therefore, after you get an object, you cannot use the this pointer through the object. So, we can't know the position of this pointer for an object (only

The position of this pointer is available in the member function. Of course, in a member function, you can know the position of this pointer (which can be obtained by &this), or you can use it directly.

F. After each class has been compiled, whether to create a function table in a class to hold function pointers so that functions can be invoked.

Normal class functions (whether they are member functions or static functions) do not create a function table to hold the function pointer. Only virtual functions are placed in the function table. However, even a virtual function, such as

The compiler can explicitly know which function is invoked, the compiler will not be called indirectly through the pointer in the function table, but will call the function directly.

G:this Whether you can assign a value

In C + +, the This pointer is implicitly declared as: X *const This, which means you cannot assign a value to this pointer; in the const member function of the X class, the type of this pointer is: const x* const, which says

This object that the Ming this pointer points to is not modifiable (that is, the data members of such an object cannot be assigned operations); Since this is not a regular variable, we cannot obtain this

Address, but the compiler can.



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.