The this Pointer in C++

In C++, the this pointer is generally used in the non-static member functions of a class. It refers to the address of the object the function is accessed through.

Let’s understand the concept.

C++ classes have mainly two types of components – properties (data) and functions. Every object, created from a class, will have a separate copy of the data members. But the functions or the execution portion will have only one copy. All object objects will share a single copy of the functions.

We know that the member functions operate on the data members of the associated object. How a member function can get hold of the data members of the associated object? That’s where the concept of this pointer comes in.

Member functions are called in the context of an object. When a member function is called, the pointer of the calling object is assigned to a hidden variable called this. Then the this pointer is implicitly passed the called function.

The member function then accesses the data members using the this pointer. That’s how it can access the very copy of the data members of the calling object.

Let’s take an example.

#include <iostream>
#include <string>
using namespace std;

class c1 {
public:
    string mem_;

    void print() {
        cout << this->mem_ << endl;
    }
};

int main() {
    c1 obj1, obj2;

    obj1.mem_ = "Object 1";
    obj2.mem_ = "Object 2";

    obj1.print();
    obj2.print();

    return 0;
}
$ g++ -o test test.cpp 
$ ./test 
Object 1
Object 2

Here in the c1 class, we have a data member, mem_, and a function print() which simply prints the data member.

Notice that we accessed the data member via the this pointer inside the print() function.

Then we created two objects, obj1 and obj2, from the c1 class. We set the member variable of the first object to ‘Object 1‘ and of the second object to ‘Object 2‘.

First we called the print() function through the first object via obj1.print(). Here the this pointer is implicitly assigned to the address of obj1 and passed that to the function. We accessed the member variable using the this pointer in print() function. As the this pointer is pointing to the first object, obj1, the value of the first object’s member variable will be printed. That’s why the first output is ‘Object 1‘.

Similar thing happened for the second function call. The print() function accessed the member variable of the second object and printed ‘Object 2‘.

The good news is that the programmers don’t need to worry about all these things. Compiler does all these internally. Even we don’t need to access the member variables using the this pointer. We did it here for the demonstration purpose only.

But still there are some scenarios where programmers need to deal with the this pointer explicitly.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

0
0
0
0
0
0