Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

this — pointer to the current object

The keyword this points to the object handled by an implicit object member function. It enables explicit member access, distinguishes a data member from a parameter with the same name, and lets a function return the current object by reference.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

this
this->member
*this
ReturnType function(this ClassName& self); // C++23 explicit object parameter

this is a prvalue expression whose value is a pointer to the current object, not the object itself. In an ordinary member function of class X, its type is X*; in a const member function, it is const X*. A volatile qualifier also affects the pointed-to type. You cannot assign a new value to this.

The expression this->member accesses a member explicitly; *this denotes the current object. In the example, this->value denotes the data member, whereas plain value in set denotes the parameter. Returning *this as Counter& enables chained calls without copying the object.

this is available in implicit object member functions and default member initializers for non-static data members. A static function has no such pointer. In C++23, a declaration such as void f(this X& self) uses this to mark an explicit object parameter; inside that function, use self, not the expression this.

This entry targets C++23. The supplied source is an evolving working draft, not a frozen C++23 publication. A repository compiler flag does not attest the production runtime environment.

Examples

#include <iostream>

class Counter {
    int value = 0;

public:
    Counter& set(int value) {
        this->value = value;
        return *this;
    }

    int get() const {
        return this->value;
    }
};

int main() {
    Counter counter;
    counter.set(7).set(9);
    std::cout << counter.get() << '\n';
}

Result

9

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources