This Keyword Notes Final
This Keyword Notes Final
Here,
functionName refers to the identifier/ name you have given to the function.
The term this-> represents the 'this' pointer and memberName refers to the
respective member of the class you are trying to access.
Whenever we call a member function in association with an object, the object’s
address is implicitly passed as a hidden first argument received in the called
member function using a special pointer known as this pointer.
In terms of usage, there are these three main uses of this keyword in
the C++ programming language. They are,
This keyword in C++ language is generally used to refer current class instance
variable.
This keyword in C++ language is generally used to declare indexers.
This keyword in C++ language is generally used to pass the current object as
the parameter to another method.
Use Case 1: Resolving Variable Shadowing (ambiguity between the instance
variable and parameter):
Let's understand the problem if we don't use this keyword by the example given
below:
class student
{
int id;
String name;
student(int id,String name)
{
id = id;
name = name;
}
void display()
{
System.out.println(id+" "+name);
}
}
Class MyPgm
{
public static void main(String args[])
{
student s1 = new student(111,"Anoop");
student s2 = new student(321,"Arayan");
s1.display();
s2.display();
}
}
Output: 0 null
0 null
In the above example, parameter (formal arguments) and instance
variables are same that is why we are using this keyword to distinguish
between local variable and instance variable.
Variable shadowing is a very common use case for the ‘this’ pointer.
Variable shadowing occurs when a class member variable and another
parameter variable or local variable have the same name. The local variable
‘shadows’ the class member variable. To reference the class member
variable, we use the ‘this’ keyword.
Syntax
int value;
public:
void demoFunction(int value)
{
this->value = value;
}
From the syntax, note that the variable that is being referenced using the
‘this’ keyword is the class member variable and the other one is the
parameter variable that is available only locally.
Example
#include <iostream>
using namespace std;
class Test
{
//this is a class member variable
string testString;
public:
//non-static member function
void setData(string testString)
{
//this is refering to the class member variable
this->testString = testString;
}
void getData()
{
cout << "The string is: " << this->testString << endl;
}
};
int main()
{
//create the object
Test test;
//call the member function
test.setData("This is a test for variable shadowing!");
test.getData();
return 0;
}
Output
The string is: This is a test for variable shadowing!
Example-2:
#include <iostream>
using namespace std;
class Employee
{
public:
int id; //data member (also instance variable)
string name; //data member(also instance variable)
float salary;
Employee(int id, string name, float salary)
{
this->id = id;
this->name = name;
this->salary = salary;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void)
{
Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of
Employee
Employee e2=Employee(102, "Nakul", 59000); //creating an object of
Employee
e1.display();
e2.display();
return 0;
}
Example-3:
#include <iostream>
using namespace std;
class Student
{
public:
int roll_no;
string name;
float marks;
Student(int x, string y, float z)
{
this->roll_no = x;
this->name = y;
this->marks = z;
}
void show()
{
cout<<"Student Name "<<roll_no<<endl;
cout<<"Student Roll "<<name<<endl;
cout<<"Student Marks "<<marks<<endl;
}
};
int main(void)
{
Student stu =Student(102, "Shishir",90);
stu.show();
return 0;
}
Example:
//example of this keyword
class Student
{
int id;
String name;
student(int id,String name)
{
this.id = id;
this.name = name;
}
void display()
{
System.out.println(id+" "+name);
}
}
Class MyPgm
{
public static void main(String args[])
{
Student s1 = new Student(111,"Anoop");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();
}
}
Output111 Anoop
222 Aryan
Use Case 2: Accessing Member Variables
The ‘this’ pointer can be also used to access member functions and
variables inside a class. If we have to refer to another member function of a
class inside another class member function, we use the ‘this’ pointer. The
syntax and the example are given below.
Syntax
void demoFunction1()
{
};
void demoFunction2() {
this->demoFunction1();
}
From the syntax, note that the variable that is being referenced using the
‘this’ keyword is the class member variable and the other one is the
parameter variable that is available only locally.
Example
#include <iostream>
using namespace std;
class Test
{
public:
//this is a public class member variable
string testString;
//non-static member function
void setData(string testString)
{
//this is refering to the class member variable
this->testString = testString;
}
void getAndPrint(string str)
{
//accessing both member variables and functions using this pointer
this->setData(str);
cout << "The string is: " << this->testString << endl;
}
};
int main()
{
//create the object
Test test;
//call the member function
test.getAndPrint("This is a test for member accession!");
return 0;
}
Output
The string is: This is a test for member accession!
Use Case 3: Accessing Objects
We can use the ‘ this’ keyword to access
the objects that are currently in memory and can further manipulate them,
we will delete the current object using a member function with the help of
the ‘this’ pointer.
Syntax
void demoFunction()
{
delete this;
}
Example
#include <iostream>
using namespace std;
class Test
{
public:
//this is a public class member variable
string testString;
//non-static member function
void setData(string testString)
{
//this is refering to the class member variable
this->testString = testString;
}
void getAndPrint(string str)
{
//accessing both member variables and functions using this pointer
this->setData(str);
cout << "The string is: " << this->testString << endl;
}
void delObject()
{
//accessing the current object and deleting it
delete this;
}
};
int main()
{
//create the object
Test test;
//call the member function
test.getAndPrint("This is a test for accessing objects!");
test.delObject();
test.getAndPrint("This is a test for accessing objects!");
return 0;
}
Output(Error)
The string is: This is a test for accessing objects!
munmap_chunk(): invalid pointer
timeout: the monitored command dumped core
sh: line 1: 321122 Aborted /usr/bin/timeout 10s main
In this example, we deleted the current object using the ‘this’ pointer, and
then again tried to use the ‘getAndPrint()’ function normally. We can see
that it raises a ‘Segmentation Fault’ error when it is run, as the object has
been deleted from the memory.