C++ has a keyword this to address this substrate. The keyword this is a pointer variable, which always contains the address of the object in question.
The member function of every object has access to a pointer named this, which points to the object itself. When a member function is invoked, it comes into existence with the value of this set to the address of the object for which it called. The this pointer can be treated like any other pointer to an object.
Using a this pointer, any member function can find out the address of the object of which it is a member. Method of accessing a member of a class from within a class using this pointer.
Sample Program
#include <iostream.h>
class test
{
int a;
public:
void setdata(int ia)
{
a = ia;
this-> = ia;
}
void showdata(void)
{
cout<<”Data accessed in normal way”<<a;
cout<<”Data accessed through this”<<this->a;
}
};
void main()
{
test my;
my.setdata();
my.showdata();
}
No comments:
Post a Comment