Computer scienceProgramming languagesC++Basics of Object-Oriented Programming

Hidden pointer *this.

7 minutes read

In the realm of C++, a fascinating question arises: "How does C++ maintain the association between objects and the methods they invoke"? After all, if you create several objects and call their methods, passing some parameters, how will the compiler know which attributes (or methods) are related to this object?
The key lies in C++'s utilization of a concealed this pointer. The this The keyword serves as a clandestine pointer that is pivotal in connecting functions to the objects that trigger them. In this topic, you'll study this issue in detail.

Understanding the this pointer

In C++, this is a keyword that refers to the current instance of the class. There's a hidden pointer called this which is passed as a hidden argument to all member function calls and is available as a local variable within the body of all functions.

The hidden this pointer does not need to be created explicitly. The compiler automatically creates it for all objects. You can always call it if you want, but in most situations, it is not necessary. Next, you will examine how and in what situations it makes sense to call it.
.

Let's look at a simple example of using this:

#include <iostream>

class MyClass {
public:
    void display() {
        std::cout << "The address of the object is: " << this << std::endl;
    }
};

int main() {
    MyClass obj;
    obj.display();

    std::cout << "The address of the object is: " << &obj << std::endl;
    return 0;
}

In the example above, we did not create any variable this (the compiler did this for us), but we can display its values (as you remember, a pointer is needed to store the address). So you can use this to get the address of the MyClass object. You can easily check this. To do this, display the address of the object in the main function through the address take operator (&). The result of the program will be (in your case, the addresses will be different):

The address of the object is: 0x304a26a98
The address of the object is: 0x304a26a98

Using this in member functions

The this pointer is used to retrieve the object's address within a member function. Here is an example of how to use this in a function to avoid name conflict between function parameters and class data members.

#include <iostream>

class Box {
public:
    int length;
    void setLength(int length) {
    // Use 'this' to distinguish class data member from function parameter
    this->length = length; 
    }

    int getLength() {
        return this->length; // Use 'this' to refer to class data member
    }
};

int main() {
    Box box;
    box.setLength(10);
    std::cout << "Length of box: " << box.getLength() << std::endl;
    return 0;
}

In this expression this->length = length; to the left of the equal sign use this->length to indicate that length is a parameter of our class. And to the right of the equal sign, length is the parameter passed to the class method.

In the above example, this is used to distinguish between the class data member length and the function parameter length.

Chaining member functions using this

Chaining member functions using this is an elegant technique in object-oriented programming, often employed in C++ and other programming languages. This technique allows you to call multiple member functions of an object in a single line, one after the other, by returning a reference to the object itself (this) from each member function. This can lead to more concise and readable code, especially when working with complex object operations.

Suppose you want to describe a box and set the width and length. In this case, we can describe our class like this:

class Box {
public:
    int length;
    int breadth;
    Box &setLength(int length) {
        this->length = length;
        return *this;
    }
    Box &setBreadth(int breadth) {
        this->breadth = breadth;
        return *this;
    }
};

int main() {
    Box box;
    box.setLength(10).setBreadth(20);
    return 0;
}

We could do this to call the code in the usual way:

box.setLength(10);
box.setBreadth(20);

But in this example, we have implemented the methods in such a way (setLength and setBreadth functions return a reference to the object via this pointer), which allows us to chain function calls (box.setLength(10).setBreadth(20);).

It may seem overkill for this example, but imagine we have many methods for retrieving data from a database or other text source. Then we can do something like this:

query.select("column1").from("table").where("column2 = 42").orderBy("column3");

The logic for calling such chains is simple: the first method on the left is executed first, the result is passed on to the next method in the chain, and so on.

Conclusion

The pointer this is an implicit parameter to all member functions. It is a pointer to the object that invokes the function. It can be used to retrieve the object's address within a member function, to resolve name conflicts between function parameters and class data members, and to chain member functions. Understanding the this pointer is crucial to mastering object-oriented programming in C++.

8 learners liked this piece of theory. 1 didn't like it. What about you?
Report a typo