C++ this pointer

Before understanding the this pointer in C++, first understand some basic concepts of C++.

1. In C++, struct is a class type. The default access control permission of class members and inheritance is private, while struct is public.

2. Static members: Class members or member functions declared with the static keyword can be shared within the scope of the class. We call such members static members and static member functions.

 this pointer

1. Definition

     An object's this pointer is not part of the object itself and does not affect the result of sizeof(object). The scope of this is inside the class. When accessing the non-static members of the class in the non-static member function of the class, the compiler will automatically pass the address of the object itself as an implicit parameter to the function. Even if you don't write the this pointer, the compiler also adds this when compiling. As an implicit parameter of a non-static member function, the access to each member is performed through this.

2. Use

    i. When returning the class object itself in the non- static member function of the class, use return *this directly;

    ii. Use the this pointer when the parameter is the same as the member variable name, such as this->n = n (can not be written as n = n).

3. Examples

    This pointer is an automatically generated, automatically hidden private member of the class, which exists in the non- static member function of the class and points to the object where the called function is located. There is only one this pointer globally. When an object is created, this pointer stores the first address of the object data.

#include <iostream>
using namespace std;
class A
{
	private:
	        int x,y;
	        int x1,y1;
	public:
	        A(int m,int n){
				x=m,x1=m;
				y=n,y1=n;
			}
	        void add(int m,int n){
				x+=m;
				y+=n;
				cout << "x=" << x << "," << "y=" << y <<endl;
			}
			void add1(int m,int n){
				this->x1+=m;
				this->y1+=n;
				cout << "x1=" << x1 << "," << "y1=" << y1 <<endl;
			}
};
intmain()
{
	A a(10,20);
	a.add(2,5);
	a.add1(2,5);
	return 0;
}</span>

    In the example, class A creates the object a of the class, and when the object a calls the function add(2,5), the first address of the object a is assigned to the this pointer. Therefore, the prototype of the add function is completed as void add(a *this, intm, int n); the first parameter is a pointer to the object of this class, we didn't see it when we defined the member function because this parameter is in the class is implicit. In this way, the address of a is passed to this, so it is explicitly written in the add function:

   void add(int a, int b) { this->x +=a; this-> y+= b;}

   That is to say, after add calls the function, the data member of add is called and the value is updated . The Add function does not take this pointer (but implicitly) and the add1 function explicitly gives the this pointer. It is found that the same result is compiled under gcc.


4. Some thoughts

     In each member function contains a special pointer, the name of this pointer is fixed, called this pointer. It is a pointer to an object of this class, and its value is the starting address of the object where the currently called member function is located.

   For example, this->x+m; this->y+m; in the example is equivalent to x+m; y+m;

  The this pointer is used implicitly, it is passed as a parameter to member functions:

int A::add()
    {

   return (x+y);

}

C++ hides the this pointer in the processing process, we restore it, which is

int A::add(A *this)

{

   return ((this->x) + (this->y));

}

Here the this pointer is added to the formal parameter list of the member function .

5. Pay attention

   A.   This pointer can only be used in member functions, global functions and static functions cannot use this pointer.

   B.   this is constructed before the start of the member function and cleared after the end of the member .

   C.   When calling a member function of a class, the compiler passes a pointer to the class as the this parameter of the function. Such as:

   A a;  a.func(10);

  Here, the compiler will compile to: A::func(&a, 10);

   D.   this is a pointer to an object of class or public.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325579579&siteId=291194637