This keyword in C++
- This keyword in C++ is a pointer which points to the object.
- It is passed as an argument to functions which helps in accessing the object. It is used for a non-static function.
- There are different ways in which we can use this keyword in C++. They are
- To resolve the shadowing issue when this keyword is used.
- To access the data members of the current object.
- To access the currently executing object using this keyword.
- To call the member function of the current object.
To resolve the shadowing issue when this keyword is used:
- Before getting into the program, we need to know what shadowing is.
- Shadowing issue occurs when both the instance variable and local variable have the same name.
- Let us consider a program in C++ which is used in case of shadowing issues:
/ / program in C++ for above case
# include< iostream >
Using namespace std;
Class demo {
string s;
public:
demo ( string s )
{
/ / using this keyword
/ / to initialize the value
this - > s = s;
}
void display ( ) {
cout << s << endl; } ];
/ / main function
int main ( )
{
Demo d(“Hello world “);
d.display ( );
return 0;
}
Output:
Hello world
To access the data members of the current object
- By using this keyword, we can access the object and the data member associated with the object.
- Let us consider a program in C++ for the above scenario:
/ / program in C++
/ / to access the object using this keyword
# include <iostream>
Using namespace std;
class demo
{
String s;
public :
demo ( string s )
{
/ / using this keyword
this - > s = s;
}
Void display ( )
{
Cout << this - > s << endl;
}
};
/ /class driver
int main ( )
{
demo d (“Hello world”);
d.display ( );
return 0;
}
Output:
Hello world
To access the currently executing object using this keyword:
- By using this keyword, we can access the currently executing object.
- We can chain function, and object can be deleted with the help of the member function.
/ / program in C++ for above scenario
# include <iostream>
Using namespace std:
class demo {
string s;
public :
demo (string s) {
/ / using this keyword to assign value
this - > s = s;
}
Void display ( ) {
cout << s << endl;
}
void delete ( ) {
delete this;
} };
/ / main function
int main ( ) {
demo *d = new demo (“Hello world “);
d - > display ( );
d - > delete ( );
return 0;
}
Let us consider a program in C++ for the above scenario:
Output:
Hello world
To call the member function of the current object:
- This keyword is used to access the object of the class.
- It is also used to access the function associated with the object.
- Let us consider a program in C++:
/ / program in C++ using this keyword
/ / to call the function of current object
# include <iostream >
using namespace std;
class demo
{
String s;
public:
demo ( string s )
{
/ / using this keyword
this - > s = s;
}
void dis ( int );
void display ( );
};
void demo : : dis (int x) {
/ / accessing the function the current object
for( int i = 0; i < x ; i++) {
this - > display ( ); }
}
Void demo : : display ( ) {
/ / access the string data member
Cout << this - > s <<endl; }
/ / main function
int main ( ) {
demo d (“Hello world “);
d.dis ( 5 );
return 0;
}
Output:
Hello world
Hello world
Hello world
Hello world
Hello world
- Hence, we can conclude that this keyword can be used to To resolve the shadowing issue when this keyword is used.
- It is used to access the data member of the current class.
- To access the object which is currently executing.
- To call the function associated with the object.