the role of this pointer
- 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.
- Return the object itself
return *this
- Compare whether this object is itself
if(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: thevoid Hello(A* this){ ...}
pointer calls the functionHello(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.