C++ Program to Show Use of This Keyword in Class


The ‘this’ keyword in C++ is very important and it is used in multiple use cases. The ‘this’ keyword or the ‘this’ pointer is used as an implicit object parameter when an object’s member function is called and refers to the invoking object. We take a look at the different use cases of the ‘this’ keyword.

Syntax

The ‘this’ keyword is used in the following way

this->variable_name;

Use Case 1: Resolving Variable Shadowing

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!

Use Case 2: Accessing Member Variables

The ‘this’ pointer can be also used to access member functions and variables inside a class. In the previous example, we saw how ‘this’ solves the variable shadowing problem. 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. In the following example, 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.

Conclusion

From the various use cases, we can see that the ‘this’ pointer is very useful for various operations in C++. The ‘this’ pointer is mainly used to access the instance variables of a currently allocated object. One thing is to be noted, that ‘this’ works on non−static member functions only. It doesn’t work on non−class functions and whenever it is used it returns the address of the invoking object.

Updated on: 13-Dec-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements