The third week of learning: this pointer

the role of this pointer
  1. The effect is that the this pointer can be used in a non-static member function to represent a pointer to the object that the function acts on.
  2. Return the object itself return *this
  3. Compare whether this object is itselfif(this == &object)

eg.

class A{
     
     
...
public:
	void Hello() {
     
     cout<<"hello"<<endl;}
...};
int main()
{
     
     
	A* P =NULL;
	P->Hello();
	...
}

hello
The essence of the output Hello function: the void Hello(A* this){ ...}
pointer calls the function Hello(P), and the output hello does not require any members of the object, so the output is correct. If you want to output members, there will be an error, because there is no point to objects

this pointer and static members function

Static member functions cannot use this pointer, because static member functions do not belong to any object, so the actual number of parameters of static member functions is what you see, there is no implicit parameter number.

Guess you like

Origin blog.csdn.net/ZmJ6666/article/details/108556476