this pointer in C++

The this pointer is a pointer accessible only within the non static member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.

Syntax:-

this.member-identifier 
this->member-identifier 


Example:-
C++ program to display the use of this pointer.
#include<iostream.h>
#include<conio.h>
class ABC{
int a;
public:
void setABC(int x)
{
this->a=x;
}
};
void main(){
ABC x,y;
x.setABC(123);
cout<<"this pointer is pointing to x obj "<<endl;
getch();
y.setABC(245);
cout<<"this pointer is pointing to y obj"<<endl;
getch();
}

No comments:

Post a Comment