c ++ appreciated in this pointer (reproduced)

C ++ class understood the this pointer

We need to understand the meaning of class. should be understood as a class type, such as int, char, is a user-defined type. With this type can declare a variable, such as int x, myclass my so on. Such as variable x has the same type int variables have myclass my type. Understand this, like the interpretation of this, my inside this is a pointer of my. If there is a variable myclass mz, mz of this is a pointer to the mz. This makes it easy to understand this type should be myclass *, it is taken to dereference * this should be a variable of type myclass.

Usually in the class definitions to use their own type variables, because at that time did not know the variable name (for generic can not fix the actual variable names), you use this indicator to use such a variable itself. 

1. this pointer usefulness:

This pointer is not a part of the target object itself, it will not affect the outcome sizeof (object).

this scope is within the class, when the non-static member of a non-static member function in the class access the class, the compiler will automatically address of the object itself as a hidden parameter passed to the function. In other words, even if you do not write on this pointer, the compiler at compile time this is also a plus, as non-static member function implicit parameter, access to the members are carried out by this. For example, calling date.SetMonth (9) <===> SetMonth (& date, 9), this helped to complete this conversion.

Within the member functions, we can directly call the member function of the object, without having to do it by the member access operator, because it is the object within the meaning of this. Direct access to any of the class members are implicitly considered this use.

The purpose of this always points to the object, so this is a constant pointer, we do not allow change saved this address

2. this pointer is used:

A situation that is, returns the class object is itself a non-static class member functions when used as return * this; Another is that when the member variable parameter names are the same, such as this-> n = n (can not be written n = n). 

Example 3. this procedure pointer:

this pointer is the address of the presence of the member function, the point where the called function class instances.

The following procedures will be described with this pointer

Copy the code

#include<iostream.h>
class Point
{ 
  int x, y;
public:
  Point(int a, int b) { x=a; y=b;}
  Void MovePoint( int a, int b){ x+=a; y+=b;}
  Void print(){ cout<<"x="<<x<<"y="<<y<<endl;}
};
void main( )
{
   Point point1( 10,10);
   point1.MovePoint(2,2);
   point1.print( );
}

Copy the code

When an object point1 call MovePoint (2,2) function, addresses will point1 object passed to this pointer.
MovePoint prototype function should be void MovePoint (Point * this, int a, int b); The first parameter is a pointer to a pointer to that object, we did not see when defining membership functions because this parameter is implicit in the class It contained. Such point1 address passed to the this, it will function in MovePoint explicitly written as:
void MovePoint (A int, int B) {this-> = X + A; this-> Y = B +;}
That can be known, after point1 call this function, that is, data member is called point1 and updated values.
That is, the process function can be written point1.x + = a;. Point1 y + = b;

 

4. With regard to this a classic pointer answer:

When you enter a house,
you can see tables, chairs, floors, etc.,
but the house you can not see the whole picture.

For instance of a class is,
you can see its member functions, member variables,
but the instance itself?
this is a pointer, it always points you in this instance itself.

Transfer from: http: //blog.csdn.net/chenyt01/article/details/51316022

 

 

Based on the introduction of this guideline

The origin pointer ================= this ====================

      A student can have more books like these books belong to the students; the same token, if there are many students together, then in order to make sure they do not get confused with the books, the best way I think it should be that every classmates have written your name on their books, so that certainly will not take the wrong.

      Similarly, multiple members of an object can be seen as the object of this book have; and in the middle of a lot of objects, in order to prove that we are a member of its own members, rather than members of other objects, we also need to these members take their names. In C ++, we use this pointer to help achieve this target, this pointer record of each object memory address, and then by operator - access to members of the object>.

================= this exemplary pointer action ====================

      Apart from anything else! We have to reflect the actual usefulness of this indicator through a program:

Copy the code

#include <the iostream> 
the using namespace STD; 
 
class A 
{ 
public: 
    int GET () const {return I;} 
    void SET (int X) {this-> X = I; COUT << "stored in this pointer memory address is: "<< endl << the this;} 
Private: 
    int I; 
}; 
 
int main () 
{ 
    a a; 
    a. Set (. 9); 
    COUT <<" a target memory address is located: "<< & a << endl ; 
    COUT << "object a saved value:" << a.get () << endl; 
    COUT << endl; 
    a b; 
    b.set (999); 
    COUT << "target memory address where b It is: "<< endl << & b; 
    COUT <<" object b is stored: "<< b.get () << endl; 
    return 0; 
}

Copy the code

 The output of this procedure are as follows:

      By this output, we can see the object and a memory address identical to the this pointer (both 0017F7E8); and when the object b to run when it has memory address and its corresponding pointer to this memory exactly the same as the address (all 0017F7DC). This explains this record is a pointer variable current memory address of the object that this pointer to the current object!

      In line 8 of the procedure, we use this property of this pointer, namely: this-> i = x; this sentence represents the value of x assigned to the private member function i of the current object.

================= ==================== summary

      By the above example, we can see the biggest role this indicator is that it contains the address of the current object, the application form and a pointer to the current object. We will see the benefits of this in another blog post .......

Transfer from: http: //www.cnblogs.com/uniqueliu/archive/2011/09/24/2189545.html

Guess you like

Origin blog.csdn.net/suyunzzz/article/details/89915905