Explain This Pointer With Example Program
This Pointer
• ‘this’ pointer :- ‘this’ pointer holds the address of the instantiated object.
• The member functions use this pointer to access the data members of a class. Internally, the address in this pointer is passed to the member functions through the global ecx register of the microprocessor.
• this pointer stores the address of the class instance, to enable pointer access of the members to the member functions of the class.
• this pointer points to the object for which the member function is currently executing.
• This unique pointer is automatically passed to member function when it is called.The pointer this acts as an implicit argument to all the member functions.
• this pointers are not accessible for static member functions.
• this pointers are not modifiable.
Example:
#include<iostream.h>
#include<conio.h>
class base1
{ int x,y;
public:
void get()
{ cout<<"enter x and y";
cin>>x>>y;
}
void show()
{ cout<<"\nx="<<x<<"\ny="<<y;
cout<<"\n"<<this;}
};
void main()
{
clrscr();
base1 b1,b2;
b1.get();
b1.show();
b2.show();
getch();
}
Labels: C++