C++

“This” Pointer C++

In this article, we will discuss the purpose of the “this” pointer and how we can use the pointer “this” in C++ programming in Ubuntu 20.04. So, we should know about the concept of a pointer in the C++ programming language first to better understand the ”this” pointer. Pointers are variables in the C++ programing language which store the memory location of other variables. The purpose of a pointer is to help us locate the variable we are working with; this helps when writing a long code with a huge number of lines. We usually do not remember every single variable, so we use pointers to help us get to that variable. Now coming to the “this” pointer in the C++ programing language, it refers to the accessibility of every object’s address in the same class or member function. The main purpose of the “this” pointer is to be used to refer to the calling object within a member function. So, we can call any local variable or local object present in our class or function by simply referring to the “this” pointer.

Uses of the “this” pointer in  C++:

The use of pointers in the C++ programing language is widespread. The majority of coders and developers have these pointers in their daily practice. This is useful when working with long codes and a huge number of lines of code in a program since it allows you to refer to an object without remembering where it is. There are many types of pointers in the C++ programing language, and all of them serve different and respective purposes accordingly to their functionalities. The “this” pointer is used in the C++ programing language for several purposes. We will discuss some of them in detail. The first use of the “this” pointer is to send the current object to another method as a parameter. So, when you are writing a code consisting of multiple methods, they are interlinked. The object of one method or even the output of one method has to be sent to the other method in the same program; it can be sent by using “this” pointer.

The “this” pointer will be beneficial as it will ease transferring their objects to one another. The “this” pointer is also used to refer to the current instance variable of the class. Whenever we create a variable, that is the member variable, in a class, the variable becomes an instance variable accessible in every part of the class, but remembering the variable for calling it and using it for its purpose will be a hectic ordeal. In this case, we use the “this” pointer to come out of this problem. So, we assign the “this” variable to these variables for the easy calling of the variable in any class instance. All member functions present in the current class take the “this” pointer as an implicit argument. As a result, the “this” pointer is also used to refer to the calling object within a member function. The “this” pointer also serves the purpose of indexers. This is where the “this” pointer comes in handy. Indexers are created in classes; thus, we can access the instance variable at any place in the class. The “this” pointer will act as an indexer when assigned to any current class instance variable.

So, if we want to remove or change some of the attributes of an instance variable in a class or function, we can just type the command in code by using the “this” pointer; for example, “delete this” will delete the current instance variable of the class. The ”this” pointer is also used in the member functions to give out all its object attributes defined in the functions by using “*” this means all the given attributes of the object. This can be done by writing this command “return *this” in the function, and it will return the object of the function.

Now let us look into some of these uses of the “this” pointer in the Ubuntu 20.04 environment to better understand these concepts by looking into the code and the output.

Using the “this” pointer in different functions in one class in Ubuntu 20.04:

Open the terminal and get to the desktop directory by writing “cd Desktop” and then make a .cpp file by writing and executing the command “touch” with the filename and extension of .cpp. Then, Locate and open your .cpp file on the desktop. Now we will write a code in that file in which we will create a class with three functions that will be interlinked with one another, and the output will be dependent on each other.

This file should be saved and then closed. Open the terminal again and compile the file with this command “g++” along with your file name and extension. This will generate an output file for your .cpp file on your desktop, after compiling, usually with the extension of “.out.” Now execute the output file by writing this command “./” along with your Output filename.

In this above example, we see how the “this” variable is referring the local data member of the object within the different functions and provides a combined output in the other function.

Using the “this” pointer when we want to use a local variable and a member variable together in Ubuntu 20.04:

Open the terminal and get to the desktop directory by writing “cd Desktop” and then make a .cpp file by writing and executing the command “touch” with the filename and extension of .cpp. Then, Locate and open your .cpp file on the desktop. Now we will write a code in that file in which we will be using the same name for local and member variables of a class and a method, respectively, so that we can inherit the attributes of the private value to the local variable and assign or edit different values to the variable in the end.

Now the .cpp file should be saved and then closed. Open the terminal again and compile the file with this command “g++” along with your file name and extension. This will generate an output file for your .cpp file on your desktop, after compiling, usually with the extension of “.out.” Now execute the output file by writing this command “./” along with your Output filename.

As you can see that the hidden variable is retrieved in the local instance variable by using the “this” pointer and using similar names.

Conclusion:

In this article, we learned about the concept and uses of the “this” pointer in the C++ programming language. Pointers itself is a very vast topic to look into in C++ programming as it helps the development by providing ease and excellence in the whole process. In this article, the usage of the “this” pointer is fully described, and the relevance of its use is demonstrated by examples in run-time circumstances in Ubuntu 20.04 environment in extensive detail. This will help you whenever you use the ”this” pointer in C++ programming in Ubuntu 20.04.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.