the this pointer points to an object so you can refer to the object in your code and change its internal state without actually knowing the client name of the object. say for instance you are defining a class worker, and you want to have each worker point to the next alphabetical worker. inside the class you have a next pointer. you can then change this pointer inside a function in the class say an nextWorker function by using this->next = "whatever". the this pointer is essential because when you are designing your class you do not know what the client will call the worker variable. also the this pointer allows you to keep the private data members private such as the next pointer and away from the client.
Class method functions differ from plain C functions in such a way, that they have an additional hidden formal parameter - a pointer to an actual object of the class, they can deal with. This pointer can be accessed through operator "this". So, if you call
my_object.my_method();
then inside of my_method() "this" gets value &my_object
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.