Image

C++ - this pointer - this pointer

this pointer

Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member function. Therefore inside a member function this may be used to refer to the invoking object.

Friend function does not have this pointer because friends are not member of class. Only member function have this pointer.

e.g:
#include<iostream.h>
#include<conio.h>
class Number
{
  int a,b;
  public:
	 Number(int a,int b)
	 {
	  this->a=a;
	  this->b=b;
	 }
	 void show()
	 {
	  cout<<"\n a = "<<a;
	  cout<<"\n b = "<<b;
	 }
};
void main()
{
 Number n(3,4);
 clrscr();
 n.show();
 getch();
}
O/P
A=3
B=4