×

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

Structure Vs Class in C++

The structure in C++ is similar to that of a class, with a few exceptions. Both the structure and the stage, the most important thing is safety. The property is...

4 minutes read.

Processing strings using std string stream in C++

A string class object called std: string stream is used to streamline into various variables, just as files can pour into strings. This class's things make use of a string...

3 minutes read.

Differences between Local and Global Variable

Define Global Variable Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks. It may be accessed by any function in...

3 minutes read.

C++ Try-Catch

Every useful program will eventually encounter unexpected outcomes. By entering data that are incorrect, users might create mistakes. Sometimes the program's creator didn't consider all of the options or was...

7 minutes read.

Malloc() and new in C++

In C ++, malloc () and new are used for the same thing. During runtime, they are used to allocate memory. Malloc () and the new, on the other hand,...

4 minutes read.

Structured Binding in C++

Structured binding is the new feature of C++ 17. It is used to bind the specified name with an element of the initializer. Structure binding is used to declare multiple...

3 minutes read.

C++ References

C++ References An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use...

3 minutes read.

Advantage and disadvantage friend function C++

Friend Function: - A friend function in C++ is a function that can access the private, protected, and public members of a class. In C++, a friend function is a function that...

2 minutes read.

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.

Sliding Window Technique in C++

Sliding Window Technique or Window Sliding Technique is a computational technique that is mainly used to reduce the use of nested loop and replace it with a single loop. It...

4 minutes read.

Design Patterns in C++

A design pattern offers an all-encompassing, repeatable answer to the typical issues that arise in software design. Usually, the pattern demonstrates the connections and interactions between several classes or objects....

10 minutes read.

Array of sets in C++

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. A collection of data objects stored in a continuous way is...

6 minutes read.

How to concatenate two strings in C++

In the C++ programming language, the concatenation of two or even more strings is covered in this section. The term "string concatenation" refers to a collection of characters that join two...

4 minutes read.

Virtual base class in C++

Consider in a C++ program, there are 4 classes named class A, class B, class C, and class D. If class B and class c inherit properties from class A....

3 minutes read.

Features and Use of Pointers in C/C++

What is a pointer? A pointer is mainly used to store the address of another variable. The * operator creates a pointer variable, which points to a data type (like an...

7 minutes read.

C++ Functions

In other programming languages, a function is referred to as a process or a subroutine. We can design functions to execute any task. A function can be used repeatedly. It...

5 minutes read.

Armstrong number using for loop in C++

What is For Loop? A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax that can be...

4 minutes read.

How to create the Processes with Fork in C++

 In this article, we are going to explain the different methods that help us to create processes with a fork(). There are two methods to do so. These methods are...

2 minutes read.

Pthreads or POSIX Threads in C++

The thread API for C/C++ is implemented by pthreads or POSIX threads. It enables the multithreading system, which enables parallel and distributed processing, and the creation of new concurrent process...

3 minutes read.

Static keyword in C++ vs Java

Both in C++ and Java, the static keyword is employed for essentially the same function. But there are some variations. The static keyword's similarities and differences between C++ and Java...

3 minutes read.