Tuesday, 17 April 2012

Write a note about this pointer. | C++

The this pointer is a pointer that exists in all non-static member functions. It points to the object for which the function is currently executing.
It is a hidden argument that is passed to every member function as if the member function were declared as shown in this example:
void Date :: myFunction(Date* this); The function declaration does not really contain the parameter when you write the code.
When your program calls a member function for an object, the compiler inserts the address of the object into the argument list for the function .
For example:
dt.myFuncti on (&dt); Once again, you do not include the object's address as an argument when you write code that calls a member function.This example shows what the compiler generates as the hidden argument for the member function's hidden this pointer parameter.
When you call a member function for an object, the compiler assigns the address of the object to the this pointer and then calls the function. Therefore, every reference to any member from within a member function implicitly uses the this pointer.

No comments: