Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Kind of a newbie question... 1

Status
Not open for further replies.

CDuddley

Programmer
Feb 4, 2002
26
US
Could some explain the 'this' pointer to me.

Thanks,
CDuddley
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top