This Pointer
Q15. Discuss about this pointer.
Answer:
This Pointer
A constant pointer contained in each object of a class points to the object for which the member function is currently executing is called as 'this' pointer. The general form or the syntax of this pointer is,
this → class members
When a member function of an object is called, the compiler assigns the address of an object to this pointer and then calls the function. The 'this' pointer is implicitly used to refer both the data members and the member function of an object. Only the non-static member functions can use the keyword.
Example
//program illustrates the use of
this pointer.
#include<iostream.h>
#include<conio.h>
class Operator
{
private:
int i;
public:
void set_data(int j)
{
i = j;
cout<<"The object's
address is "<<this;
this → i =j;
}
void display()
{
cout<<"\n"<<i;
cout<<"\n The object's
address is "<<this;
cout<<this → i;
}
};
void main()
{
Operator op;
clrscr();
op.set_data(10);
op.display( );
getch();
return 0;
}
Output .