Here, we will see how to use this keyword in a class using a C++ program. this keyword in C++ is an implicit pointer that points to the object of the class of which the member function is called. Every object has its own this pointer. Every object can reference itself by this pointer.
There are 4 ways this keyword can be used in a class in C++:
- Resolve Shadowing Issue Using this Keyword.
- Access Currently Executing Object Using this Keyword.
- Access Data Members Using this Keyword.
- Calling Member Functions Using this Keyword.
Let’s start discussing these different ways in detail.
1. Resolve Shadowing Issue Using this Keyword
Shadowing occurs when there is a local variable that has the same name as an instance variable. Below is the C++ program to show how this keyword can be used to resolve the shadowing issues:
C++
#include <iostream>
using namespace std;
class GFG
{
string name;
public :
GFG(string name)
{
this ->name = name;
}
void display()
{
cout << name << endl;
}
};
int main()
{
GFG gfg( "GeeksforGeeks" );
gfg.display();
return 0;
}
|
2. Access Currently Executing Object Using this Keyword
This keyword can be used to chain functions and delete objects via its member functions.
Example 1: Below is the C++ program to use this keyword to delete the object using its member functions.
C++
#include <iostream>
using namespace std;
class GFG
{
string name;
public :
GFG(string name)
{
this ->name = name;
}
void display()
{
cout << name << endl;
}
void del()
{
delete this ;
}
};
int main()
{
GFG *gfg = new GFG( "GeeksforGeeks" );
gfg->display();
gfg->del();
return 0;
}
|
Example 2: Below is the C++ program to use this keyword to access currently executing object to chain function calls:
C++
#include <iostream>
using namespace std;
class GFG
{
string name;
int data;
public :
GFG setName(string name)
{
this ->name = name;
return * this ;
}
GFG setData( int data)
{
this ->data = data;
return * this ;
}
void display()
{
cout << name << endl;
cout << data << endl;
}
};
int main()
{
GFG gfg;
gfg = gfg.setName( "GeeksforGeeks" ).setData(20);
gfg.display();
return 0;
}
|
3. Access Data Members Using this Keyword
Below is the C++ program to use this keyword to access the data member of the currently executing object:
C++
#include <iostream>
using namespace std;
class GFG
{
string name;
public :
GFG(string name)
{
this ->name = name;
}
void display()
{
cout << this ->name << endl;
}
};
int main()
{
GFG gfg( "GeeksforGeeks" );
gfg.display();
return 0;
}
|
4. Calling Member Functions Using this Keyword
Below is the C++ program to use this keyword to call member functions associated with the currently executing objects:
C++
#include <iostream>
using namespace std;
class GFG
{
string name;
public :
GFG(string name)
{
this ->name = name;
}
void displayX( int );
void display();
};
void GFG :: displayX( int x)
{
for ( int i = 0; i < x; i++)
{
this ->display();
}
}
void GFG :: display()
{
cout << this ->name << endl;
}
int main()
{
GFG gfg( "GeeksforGeeks" );
gfg.displayX(4);
return 0;
}
|
OutputGeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks