Understanding and function of C++ this pointer in detail

  • 2020-06-19 11:28:27
  • OfStack

01 C++ program to C program translation

To understand the C++ this pointer, let's first convert the following C++ code into C code


class Car 
{
public:
 int m_price;   //  Member variables 
 void SetPrice(int p) //  A member function 
 {
  m_price = p; 
 }
};

int main()
{
 Car car;
 car.SetPrice(20000); //  to car object m_price Member variable assignment 
 
 return 0;
}

The C language is an class keyword without a class definition, but it has a definition similar to class, which is the structure struct.

The m_price variable is a member of the Car class, so we can translate the Car class and member variables into the following C code:


//  The structure of the body Car
struct Car
{
 // price The variable belongs to Car Struct is a variable in the domain 
 int price; 
};

SetPrice function is a member function of Car class, but there is no member function in C program, so the member function can only be translated into global functions:


//  parameter 1 : structure Car A pointer to the 
//  parameter 2 : The price variable to set 
void SetPrice(struct Car* this, int p)
{ 
 this->price = p; //  Will the incoming Car Structure of the price Variable assignment 
}

Why add a pointer to this? Let's keep going.

Here we translate C++ from main above to C below:


int main() 
{
 struct Car car;
 SetPrice( &car, 20000);
 return 0;
}

Therefore, the code to convert the above C++ program into C program is as follows:


struct Car
{
 int price; 
};


void SetPrice(struct Car* this, int p)
{ 
 this->price = p; 
}

int main() 
{
 struct Car car;
 SetPrice( &car, 20000); //  to car Structure of the price Variable assignment 
 return 0;
}

02 this pointer in action

What it does is it points to the object that the member function is acting on,

So a non-static member function can directly use this to represent a pointer to the object that the function is acting on.


#include <iostream>

class Car 
{
public:
 int m_price;
 
 void PrintPrice()
 {
  std::cout << m_price << std::endl; 
 }
 
 void SetPrice(int p)
 {
  this->m_price = p; //  Is equivalent to  m_price = p;
  this->PrintPrice();//  Is equivalent to  PrintPrice();
 }
 
 Car GetCar()
 {
  return *this; //  Returns the object the function is applied to 
 }
};

int main(void)
{
 Car car1, car2;
 car1.SetPrice(20000);
 
 // GetCar() The member function returns the function applied to car1 Object that will be returned car1 Assigned to car2
 car2 = car1.GetCar(); 
 car2.PrintPrice(); 
 
 return 0;
}

Output results:

[

20000
20000

]

What do you think the output will be in the following code? Can you make a mistake?


class A
{
 int i;
 public:
 void Hello() { cout << "hello" << endl; }
};

int main()
{
 A * p = NULL;
 p->Hello(); // What's the result? 
}

The answer is to output hello normally. You may be wondering if p should crash when the pointer is empty. Don't worry, let's convert the above code to C first, then we can understand why it works.


void Hello() { cout << "hello" << endl; } 
#  The member function corresponds to the following form: 
void Hello(A * this ) { cout << "hello" << endl; }

p->Hello(); 
#  perform Hello() Form equivalent to: 
Hello(p);

Therefore, in fact, the first parameter of every member function has a pointer to the object by default. In the above case, if the object is empty, the first parameter of the member function is NULL, so long as the member function does not use the member variable, it can be executed normally.

When this code is executed, it crashes because the this pointer is empty and the null pointer is used to point to the member variable i, and the program crashes.


class A
{
  int i;
public:
  void Hello() { cout << i << "hello" << endl; }
  // ->> void Hello(A * this ) { cout << this->i << "hello" << endl; }
};
int main()
{
  A * p = NULL;
  p->Hello(); // ->> Hello(p); 
}

03 this Pointers and static member functions

Static member functions cannot use this Pointers because static member functions are equivalent to Shared variables, not variables belonging to an object.

04 summary

By translating C++ program into C program, you can understand this pointer, which is used to point to the object of non-static member function. The first parameter of each member function is actually a default this pointer parameter.

Static member function is unable to use this pointer,

conclusion


Related articles: