Skip to content

Last updated:2026-10-12

C++ this Pointer

About the Author

WeChat official account:

wechat

1. The this Pointer

Many of you, I'm sure, are learning Python. Python has self; the C++ analogue is the this pointer. So let's dig into how the this pointer is used inside a class!

First, let's talk about what the this pointer is for:

(1) An object's this pointer is not part of the object itself and does not affect the result of sizeof(object).

(2) this lives inside the class scope: when a non-static member function accesses a non-static member of the class, the compiler automatically passes the address of the object itself to the function as an implicit argument. In other words, even if you do not write the this pointer explicitly, the compiler adds it at compile time. As an implicit formal parameter of every non-static member function, all member access goes through this.

Next, how to use the this pointer:

(1) When returning the class object itself from a non-static member function of the class, simply use return *this.

(2) When a parameter has the same name as a member variable, write this->n = n (it cannot be written as n = n).

Also, online you may see this being resolved by the compiler as A *const or A const * — which one is it? Let's find out by setting a breakpoint and debugging:

Given the following example:

#include<iostream>
#include<cstring>


using namespace std;
class Person{
public:
    typedef enum {
        BOY = 0, 
        GIRL 
    }SexType;
    Person(char *n, int a,SexType s){
        name=new char[strlen(n)+1];
        strcpy(name,n);
        age=a;
        sex=s;
    }
    int get_age() const{

        return this->age; 
    }
    Person& add_age(int a){
        age+=a;
        return *this; 
    }
    ~Person(){
        delete [] name;
    }
private:
    char * name;
    int age;
    SexType sex;
};


int main(){
    Person p("zhangsan",20,Person::BOY); 
    cout<<p.get_age()<<endl;
    cout<<p.add_age(10).get_age()<<endl;
    return 0;
}

For this simple program, surely there is no problem: a class is defined, then the constructor initializes it, and we get the person's age, set it, and get it again!

To verify which one the this pointer is, we now set a breakpoint at add_age and run:

thiscontrust

genthis

You will find that the compiler automatically gives us A* const, not A const *this!

Next, there is also a const function above, so let's set a breakpoint on get_age, as follows:

constthis

You will find that the compiler turns the this above into const A* const. You could have guessed this: since the function is a const function, it can only access const variables and const functions and must not modify the values of other variables, so it needs a this that points to variables that cannot be modified — that is, const A*. And since this itself is a const pointer, the result is const A* const!

Summary: this is constructed before execution of a member function begins and cleared after the member's execution ends. The get_age function above gets resolved to get_age(const A * const this), and add_age gets resolved to add_age(A* const this, int a). In C++ classes and structs differ in only one way: class members are private by default, while struct members are public. this is a pointer to the class; if it were a struct, this would be a pointer to the struct.

Comments