×

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
    1. To resolve the shadowing issue when this keyword is used.
    2. To access the data members of the current object.
    3. To access the currently executing object using this keyword.
    4. 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.

Related Topics

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...

3 minutes read.

Bitmasking in C++

Bitmasking is a technique that is used to access a particular bit within the data bytes. When you apply the iterative approach, this phenomenon is used. A bitmask is described...

6 minutes read.

std::distance() in C++

The primary function of std::distance is to facilitate the total number of elements if we have two iterators. It is defined inside the header files. It has both magnitude and...

2 minutes read.

C++ find missing in the second array

Given two arrays A and B of sizes n and m. Find the elements from array A that are not present in array B. Example Input : a[] = {1, 2, 3, 5,...

3 minutes read.

rand() and srand() in C / C++

In this tutorial, we'll explore the syntax, usage, and examples of the C++ STL functions rand() and srand(). What exactly is rand()? The C++ STL's built-in rand() function is defined in the...

3 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

Type difference of Character literals in C VS C++

Character literals in C: In C, a character literal is represented by a single character enclosed in single quotes, such as 'a' or 'b'. It is of type int. This means...

5 minutes read.

How to create a directory or folder in C/C++?

A directory is to lists all files and subdirectories in a directory, set of the files will be kept in the directory, which is a location. A subdirectory is a...

5 minutes read.

How to Reverse a String in C++ using While Loop

A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the condition no longer...

6 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

Reverse a String using Stack C/C++

Reverse the given string using stack. To turn "tutorialandexample" into "elpmaxednalairotut," for instance. Here is a straightforward stack-based technique for reversing strings. Algorithm: 1) Make a stack that is empty. 2) Push each character...

3 minutes read.

C++ Dijkstra Algorithm Using the Priority Queue

In this article we will be finding the shortest routes from a source vertex in a graph to all vertices in the graph, given a graph and a source vertex...

5 minutes read.

C++ vs C#

What exactly is C++ programming? Bjorne Stroustrup is the creator of the C++ programming language. His goal was to create a powerful object-oriented programming language with the capabilities of C. It...

4 minutes read.

C++ Mutable keyword

C++ mutable keyword Mutable class storage specifier in C++ Storage class specifiers in C are auto, register, static, and external. Typedef is also considered as a specifier of the storage...

4 minutes read.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

C++ this pointer

'this' is a pointer that points to the object for which this function was called. The 'this' pointer holds the memory address of the current object. The 'this' pointer is implicitly passed to...

2 minutes read.

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 minutes read.