C++ this | Working of “this” Pointer in C++ with Examples

C++ thisC++ this

Introduction to C++ this

In C++, this is a hidden pointer which can access the objects in C++ program with its address and it is done by using the “this” keyword as it is an implicit argument that can be passed to all the member functions for referring to the invoking object except the friend functions as they are not members of the class. In general, we can define “this” in C++ as a pointer that is passed as an argument to nonstatic member functions where to access the object having a single copy of member functions the compiler provides this hidden pointer implicitly by using the keyword “this” followed by the function name because static member function does not use this pointer.

Working of “this” Pointer in C++ with Examples

In this article, we will see “this” pointer which is a hidden pointer provided by the compiler to access the program objects with its address though it can be accessed by the multiple objects and to access the proper value members then the compiler implicitly defines the “this” pointer along with the function name. In C++, the “this” pointer can store the address of the present or current pointer which points to the current object or an instance of the class.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

We should be careful when we are using this pointer in the constructor which means in the {body} and also when we are initializing the list and therefore it is a practice or may developers say we should not use this pointer in a constructor as the objects of the class are not yet formed completely as it’s just the beginning or initialization part. In C++, this pointer is mainly used for accessing or referring the current instance variable of a class, this pointer is also used for passing the parameters which are current objects to any other method in the program, and lastly, this pointer is used for declaring the indexers in C++.

Now we will see how to declare it or syntax of this pointer in C++:

Syntax:

This.member_identifier;

In C++, this pointer is used when the data members and the local variables of the member function have the same name then the compiler will be in ambiguity until an unless we use this pointer because if we want to assign some values of a local variable to the data members then this cannot be done without this pointer.

Example #1

Code:

#include<iostream>
using namespace std;
class loc_mem_same
{
private:
int i;
float f;
char c;
public:
void name_var (int i, float f,char c)
{
this->i = i;
this->f = f;
this->c = c;
}
void display_values()
{
cout << "The integer value is = " << i << endl;
cout << "The float value is = " << f << endl;
cout << "The character value is = " << c << endl;
}
};
int main()
{
loc_mem_same inst;
int i = 20;
float f = 2.05890;
char c = 'E';
cout << "The display of values which have both local variable and data member have same name and using this pointer" << endl;
inst.name_var(i,f,c);
inst.display_values();
return 0;
}

Output:

C++ this 1C++ this 1

In the above program, we can see we have declared 3 local variables i, f, and c in the member function “name_var” which are the same names as the data member declared in the private part int i, float f, and char c. So to refer these names compiler will not be able to refer without using this pointer. In the above program we have referred them like this -> i, this-> f, and this-> c. And if this pointer is not used then we will get some garbage value.

In C++, this pointer can be used for calling all the functions for the present instance at once by just returning the reference of the current object and we can continue to call the functions using these current objects. In general, this reference returned by the local object can be used as a single object by chaining the function calls using this pointer.

Example #2

Code:

#include<iostream>
using namespace std;
class ref_func
{
private:
int i;
float f;
char c;
public:
ref_func(int i, float f, char c)
{
this->i = i;
this->f = f;
this->c = c;
}
ref_func &seti(int p)
{
i = p;
return *this;
}
ref_func &setf(float q)
{
this->i= i++;
f = q;
return *this;
}
ref_func &setc(char r)
{
c = r;
return *this;
}
void disp_val()
{
cout << "The integer value is = " << i << endl;
cout << "The float value is = " << f << endl;
cout << "The character value is = " << c << endl;
}
};
int main()
{
cout << "Demonstration for returning current instance from a member function using this pointer " << endl <<"\n";
ref_func inst(30, 4.098, 'T');
inst.seti(4).setf(20.587).setc('e');
inst.disp_val();
return 0;
}

Output:

for returning current instancefor returning current instance

In the above program, we can see we have declared the local variables and we can use this pointer in the above to return the reference of the current instance or object of the class. In the above program we can see in the first function it will give what value we have assigned in the main for “i” which would give output as “30” but in the second function inside the float function we have incremented the “i” value so it should again give us the value as “31” but in the main, we are again assigning some other values to the variables and now we have assigned “i” with 4. So now in the output, we will get the “i” value is “4”. So in this way, we can say we are referring to the value of the current or recent object in the class.

Conclusion

In this article, we conclude that this pointer in C++ is like a hidden pointer where the compiler implicitly declares this pointer with the keyword and is followed by the function name. In this article, we saw how this pointer is used for accessing the local variable’s values even when the data member names and local variables names are the same using the “this->” pointer and we also how we can refer to the latest value assigned to the variable or current object of the class using “this*” pointer. In this article, we saw examples of demonstrating these two situations. Also, note it when we are using this pointer in the constructor we need to be careful as the objects may not be formed yet.

Recommended Articles

This is a guide to C++ this. Here we discuss the introduction and working of “this” pointer in C++ with examples respectively. You may also have a look at the following articles to learn more –

0

Shares

Share